home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Interapplication Communication / AE Sample Applications / 7Edit / C Sources / SVAppleEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-23  |  162.7 KB  |  5,937 lines  |  [TEXT/MPS ]

  1. /* 
  2.     SVAppleEvents.c 
  3.   
  4.     Version 3.0d9
  5.     
  6.     Copyright © SRL Data 1992, 1993
  7.     
  8.     All rights reserved
  9.     
  10.     Produced by : SRL Data
  11.     Originally Developed for UK.DTS
  12.     
  13.     The AppleEvent handling routines for the 7Edit example program
  14.     
  15.     This file includes :
  16.     
  17.         a) the code for the AppleEvent initialisation 
  18.         b) the routines for all the Edition Manager events
  19.         c) the Apple® Event Object Support
  20.  
  21. */
  22.  
  23. /* New for 3.0d1 :
  24.         Recommenting of many functions to improve clarity.
  25.         Implementation of nearly all menu commands via Apple Events.
  26.         Page Setup on each window (with visible page breaks)
  27.         Show Borders on each window with bug fixes in display.
  28.         
  29.     New for 3.0d2 :
  30.         19-Feb-92 : NH : Implement printing
  31.                      Fix drawing on scrollbar on show page breaks
  32.                                          Make IssueAEOpenDoc a function (for printing)
  33.                                          Make Paste Styled
  34.         
  35.         21-Feb-92 : NH : Use new (in SVEditions.p) calls for updating section info.
  36.         
  37.         27-Feb-92 : NH : SendTextMessage zapped and other routines used solely by it.
  38.         
  39.         
  40.          5-Mar-92 : NH : Added HandleCreatePub, IssueCreatePub
  41.         18-Mar-92 : NH : Zap PoseMessageDialog& mod AdornDefault
  42.         27-Mar-92 : NH : Call AssocAllSections when saved
  43.         
  44.         Start of updating to constants in Winter AERegistry.
  45.         
  46.   New for 3.0d3 :
  47.         16-Jun-92 : NH : Continue updating to match latest registry.
  48.                          typeText gone - now typeChar
  49.                                          classes of kAECut etc. changed (see registry)
  50.                                          Added pIsModified code.
  51.         17-Jun-92 : NH : Modify HandleSetData - Use AEResolve to yield tokenDescriptor, then
  52.                          extract appropriate token data.
  53.         18-Jun-92 : NH : Tidy up
  54.                                          Fix AEDescs being left after send of events
  55.                          Ensure SendAESetObjProp zaps all supplied params - now VAR params
  56.                                          Zap AEDescs left behind in HandleGetData.
  57.         24-Jun-92 : NH : Zap AddNamedStyleItem, AddNamedStyleItem, BuildStyleDescList
  58.                          Add code for typeTextStyles as per registry.
  59.                                          DoOpenDocument fixed to cope with no files in list etc.
  60.                                          Fixed buggy cast of windowToken to DPtr in DoCloseWindow
  61.                                          HandleShowSelection modified to take textTokens and WindowTokens
  62.                                          
  63.         25-Jun-92 : NH : StyleToName zapped
  64.         
  65.     Changes for 3.0d4 :
  66.        3-Jul-92 : NH : Fix SetWindowProperty typeMyText
  67.          7-Jul-92 : NH : Add missing window props - pIsFloating, pHasCloseBox, pIsZoomable
  68.         13-Jul-92 : NH : Complete the menu classes.
  69.                          Allow -ve absolute positions - i.e. last line of ...
  70.                                          Change Line access to allow relative offsets
  71.       14-Jul-92 : NH : Change region massages for window positions (thanks huggy!)
  72.         15-Jul-92 : NH : Modified HandleGetData/SetData to allow styledText and typeChar
  73.                          Recording of keyStrokes
  74.         17-Jul-92 : NH : Enter key -> compile text - filtered out too
  75.                          Fix setting of styles so that styles not in on/off styles untouched
  76.          5-Aug-92 : NH : Add pUserSelection
  77.                                           Zap cSelection
  78.                                          Added MakeSelectedTextObject for Gustav
  79.                                          Styles made to match behaviour in Inside Mac VI
  80.                                          Fixed setting of styles to match registry
  81.                                          Fixed saving to work after name change of window
  82.                                          Mark document as dirty after SetData
  83.                                          Feed back all filing errors
  84.                                          Moved some data from descriptor procedures to SVEditAEUtils.c
  85.                                          
  86.     Changes for 3.0d5: 
  87.         10-Aug-92 : NH : Fixed Quit save no - AEInteractWithUser called now
  88.                                          Added IssueQuitCommand.
  89.                                          Install cLine from typeMyText accessor
  90.     Changes for 3.0d6: 
  91.     
  92.         9-Sep-92 : JL : Added a check for gRecording implemented when sending an
  93.                                         event to ourselves for recording.
  94.     
  95.     Changes for 3.0d7: 
  96.         16-Nov-92 : JL : Changed pUserSelection to pSelection
  97.         
  98.     Changes for 3.0d9
  99.         03-Dec-93     <smd>    modified code to compile with universal headers
  100.                                             (powerpc compatible).
  101. */
  102.  
  103. #include <AppleEvents.h>
  104. #include <LowMem.h>
  105. #include <Menus.h>
  106. #include <PLStringFuncs.h>
  107. #include <Scrap.h>
  108. #include <TextEdit.h>
  109. #include <AEObjects.h>
  110. #include <AEPackObject.h>
  111. #include <AERegistry.h>
  112. #include "SVEditGlobals.h"
  113. #include "SVEditUtils.h"
  114. #include "SVEditAEUtils.h"
  115. #include "SVEditWindow.h"
  116. #include "SVEditFile.h"
  117. #include "SVAppleEvents.h"
  118.  
  119. /* these should come from the registry */
  120.         
  121. #define         kAEStartedRecording   'rec1'
  122. #define         kAEStoppedRecording        'rec0'
  123. #define         kAEDontExecute                 0x00002000
  124.  
  125. #define         pText                          'TEXT'
  126. #define     cSpot             'cspt'
  127.  
  128. /*
  129.     Text Properties
  130. */
  131.         
  132. #define         pStringWidth            'pwid'
  133.         
  134. /*
  135.     Window Properties - See the Registry for Details
  136. */
  137.         
  138. #define     pPosition                'ppos'
  139. #define         pPageSetup                'PSET' /* One of ours - Not in registry */
  140. #define         pShowBorders            'PBOR' /* Another of ours */
  141.                 
  142. #define         typeTPrint                'TPNT' /* A raw TPrint record - also one of ours */
  143.                         
  144. /*
  145.     Error Codes
  146. */
  147.         
  148. #define         kAEGenericErr    -1799
  149.  
  150. static short   gBigBrother;
  151. static char    *gTypingBuffer;
  152. static short   gCharsInBuffer;
  153. static AEDesc  gTypingTargetObject;
  154.  
  155. /*-----------------------------------------------------------------------*/
  156. /**----------                         APPLE EVENT HANDLING                     ---------------**/
  157. /*-----------------------------------------------------------------------*/
  158.  
  159. pascal OSErr GetTHPrintFromDescriptor(const AEDesc *sourceDesc, THPrint *result)
  160.   {
  161.       OSErr   myErr;
  162.         OSErr   ignoreErr;
  163.         Size    ptSize;
  164.         AEDesc  resultDesc;
  165.         
  166.         myErr = AECoerceDesc(sourceDesc,typeTPrint,&resultDesc);
  167.         
  168.         *result = nil;
  169.         
  170.         if (myErr==noErr) 
  171.             {
  172.                 *result = (THPrint)NewHandle(sizeof(TPrint));
  173.                 
  174.                 PrOpen();
  175.                 PrintDefault(*result);
  176.                 
  177.                 HLock((Handle)*result);
  178.     
  179.                 GetRawDataFromDescriptor(&resultDesc,
  180.                                                                  (Ptr)**result,
  181.                                                                  sizeof(TPrint),
  182.                                                                  &ptSize);
  183.                                                                  
  184.                 HUnlock((Handle)*result);
  185.                 
  186.                 if ((ptSize<sizeof(TPrint)) || (PrValidate(*result)))
  187.                     {
  188.                         myErr = errAECoercionFail;
  189.                         DisposHandle((Handle)*result);
  190.                         *result = nil;
  191.                     }
  192.                 
  193.                 PrClose();
  194.             }
  195.         
  196.         if (resultDesc.dataHandle) 
  197.             ignoreErr = AEDisposeDesc(&resultDesc);
  198.             
  199.         return(myErr);
  200.     } /*GetTHPrintFromDescriptor*/
  201.  
  202. /*******************************************************************************/
  203. /*
  204.     Object Accessors - Utility Routines
  205. */
  206.  
  207. #pragma segment ObjectAccessors
  208.  
  209. pascal WindowPtr WindowNameToWindowPtr(StringPtr nameStr)
  210. /* 
  211.     Returns the WindowPtr of the window with title nameStr
  212.     or nil if there is no matching window.
  213. */
  214.     { 
  215.         WindowPtr theWindow;
  216.         Str255    windTitle;
  217.             
  218.         theWindow =(WindowPtr)LMGetWindowList();
  219.         /* 
  220.             iterate through windows - we use WindowList 'cos we could
  221.             have made the window invisible and  we lose it - so we
  222.             can't set it back to visible!!
  223.         */
  224.         while (theWindow)
  225.             {
  226.                 GetWTitle(theWindow, windTitle);
  227.                 if (EqualString(windTitle,
  228.                                 nameStr,
  229.                                                 false,
  230.                                                 true))     /* ignore case, don't ignore diacriticals */
  231.                     return(theWindow);
  232.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  233.             }
  234.         return(theWindow);
  235.     }    /* WindowNameToWindowPtr */
  236.  
  237. pascal WindowPtr GetWindowPtrOfNthWindow(short index)
  238. /* returns a ptr to the window with the given index
  239.   (front window is 1, behind that is 2, etc.).  if
  240.   there's no window with that index (inc. no windows
  241.   at all), returns nil.
  242. */
  243.   {
  244.       WindowPtr theWindow;
  245.         
  246.         theWindow = (WindowPtr)LMGetWindowList();
  247.     
  248.         /* iterate through windows */
  249.         
  250.         while (theWindow)
  251.             {
  252.                 index --;
  253.                 if (index <= 0) 
  254.                     return(theWindow);
  255.                     
  256.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  257.             }
  258.         return(nil);
  259.     }    /* GetWindowPtrOfNthWindow */
  260.     
  261. pascal short CountWindows(void)
  262.     {
  263.         WindowPtr theWindow;
  264.         short     index;
  265.                 
  266.         index = 0;
  267.         theWindow = (WindowPtr)LMGetWindowList();
  268.     
  269.         /* iterate through windows */
  270.         
  271.         while (theWindow)
  272.             {
  273.                 index++;                    
  274.               theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
  275.             }
  276.         
  277.         return(index);
  278.         
  279.     } /*CountWindows*/
  280.     
  281. pascal short ItemForNamedFont(Str255 theName)
  282.     {
  283.       Str255  itemName;
  284.         short   limit;
  285.     
  286.         limit = CountMItems(myMenus[fontM]);
  287.         while (limit>0)
  288.             {
  289.                 GetItem(myMenus[fontM],limit, itemName);
  290.                 if (IUEqualString(theName, itemName)==0) 
  291.                     return(limit);
  292.                 else
  293.                     limit--;
  294.             }
  295.         return(0);
  296.     } /*ItemForNamedFont*/
  297.  
  298. /**-----------------------------------------------------------------------
  299.         Name:             DoOpenApp
  300.         Purpose:        Called on startup, creates a new document.
  301.     -----------------------------------------------------------------------**/
  302.  
  303. #pragma segment Main
  304.  
  305. pascal OSErr DoOpenApp(const AppleEvent *message,const AppleEvent *reply,long refcon)
  306.   {
  307. #pragma unused (reply,refcon, message)
  308.     
  309.       DPtr ourDoc;
  310.     
  311.         /*just create a new document*/
  312.         ourDoc = NewDocument(false);
  313.         
  314.         if (ourDoc)
  315.             {
  316.                 ShowWindow(ourDoc->theWindow);
  317.                 return(noErr);
  318.             }
  319.         else
  320.             return(-108);
  321.     }
  322.  
  323. /**-----------------------------------------------------------------------
  324.         Name:             DoOpenDocument
  325.         Purpose:        Open all the documents passed in the Open AppleEvent.
  326. -----------------------------------------------------------------------**/
  327.  
  328. #pragma segment Main
  329.  
  330. pascal OSErr DoOpenDocument(const AppleEvent *message, const AppleEvent *reply, long refcon)            
  331.   {
  332. #pragma unused (reply, refcon)
  333.  
  334.         long        index;
  335.         long        itemsInList;
  336.         AEKeyword   keywd;
  337.         OSErr       err;
  338.         OSErr       ignoreErr;
  339.         AEDescList  docList;
  340.         long        actSize;
  341.         DescType    typeCode;
  342.         FSSpec      theFSSpec;
  343.         
  344.         /*open the specified documents*/
  345.         
  346.         docList.dataHandle = nil;
  347.         
  348.         err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
  349.         
  350.         if (err==noErr)
  351.             err = AECountItems( &docList, &itemsInList) ;
  352.         else
  353.           itemsInList = 0;
  354.             
  355.         for (index = 1; index <= itemsInList; index++)
  356.             if (err==noErr)
  357.                 {
  358.                     err = AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
  359.                                                          (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize ) ;
  360.                     if (err==noErr)
  361.                       err = OpenOld(theFSSpec);
  362.                 }
  363.     
  364.       if (docList.dataHandle)
  365.             ignoreErr = AEDisposeDesc(&docList);
  366.             
  367.         return(err);
  368.     }
  369.  
  370. /**-----------------------------------------------------------------------
  371.         Name:             MyQuit
  372.         Purpose:        Quit event received- exit the program.
  373.     -----------------------------------------------------------------------**/
  374.  
  375. #pragma segment Main
  376.  
  377. pascal OSErr MyQuit(const AppleEvent *message,const AppleEvent *reply,long refcon)            
  378.     {
  379. #pragma unused (reply,refcon)
  380.     
  381.         DescType saveOpt;
  382.         OSErr    tempErr;
  383.         OSErr    myErr;
  384.         DescType returnedType;
  385.         long     actSize;
  386.         
  387.         saveOpt = kAEAsk; /* the default */
  388.         tempErr = AEGetParamPtr(message,
  389.                                                         keyAESaveOptions,
  390.                                                         typeEnumerated,
  391.                                                         &returnedType,
  392.                                                         (Ptr)&saveOpt,
  393.                                                         sizeof(saveOpt),
  394.                                                         &actSize);
  395.         
  396.         if (saveOpt != kAENo)
  397.             myErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  398.  
  399.       if (myErr == noErr)
  400.             DoQuit(saveOpt);
  401.         
  402.         return(myErr);
  403.     }
  404.  
  405. /**-----------------------------------------------------------------------
  406.         Name:             DoAppleEvent
  407.         Purpose:        Process and despatch the AppleEvent
  408.     -----------------------------------------------------------------------**/
  409.  
  410. #pragma segment Main
  411.  
  412. pascal void DoAppleEvent(EventRecord theEvent)
  413.   {
  414.       OSErr err;
  415.     
  416.       /*should check for your own event message types here - if you have any*/
  417.         
  418.         err = AEProcessAppleEvent(&theEvent);
  419.     }
  420.  
  421. /**-----------------------------------------------------------------------
  422.         Name:             MakeSelfAddress
  423.         Purpose:        Builds an AEAddressDesc for the current process
  424.     -----------------------------------------------------------------------**/
  425.     
  426. pascal OSErr MakeSelfAddress(AEAddressDesc *selfAddress)
  427.   {
  428.       ProcessSerialNumber procSerNum;
  429.     
  430.         procSerNum.highLongOfPSN = 0;
  431.         procSerNum.lowLongOfPSN  = kCurrentProcess;
  432.         
  433.         return(AECreateDesc(typeProcessSerialNumber,
  434.                                                 (Ptr)&procSerNum,
  435.                                               sizeof(procSerNum),
  436.                                                 selfAddress));
  437.         
  438.     } /* MakeSelfAddress */
  439.  
  440. /**--------------------------------------------------------------------
  441.     Name : SendAESetObjProp
  442.     Function : Creates a property object from an object,
  443.                          a property type and its data and sends it to
  444.                          the requested address, and cleans up zapping params too
  445.     --------------------------------------------------------------------**/
  446.  
  447. pascal OSErr SendAESetObjProp(AEDesc        *theObj,
  448.                                                             DescType      theProp,
  449.                                                             AEDesc        *theData,
  450.                                                             AEAddressDesc *toWhom)
  451.                                                      
  452.     {
  453.        AEDesc     propObjSpec;
  454.          AppleEvent myAppleEvent;
  455.          AppleEvent defReply;
  456.          OSErr      myErr;
  457.          OSErr      ignoreErr;
  458.          AEDesc     theProperty;
  459.             
  460.         /* create an object spec that represents the property of the given object */
  461.         
  462.         myErr = AECreateDesc(typeType,
  463.                                                  (Ptr)&theProp,
  464.                                                  sizeof(theProp),
  465.                                                  &theProperty);
  466.         if (myErr==noErr)
  467.             myErr = CreateObjSpecifier(cProperty,
  468.                                                                  theObj,
  469.                                                                  formPropertyID,
  470.                                                                  &theProperty,
  471.                                                                  true,
  472.                                                                  &propObjSpec);    
  473.             
  474.         /* create event */
  475.         
  476.         if (myErr==noErr)
  477.             myErr = AECreateAppleEvent(kAECoreSuite,
  478.                                                                  kAESetData,
  479.                                                                  toWhom,
  480.                                                                  0,
  481.                                                                  0,
  482.                                                                  &myAppleEvent);
  483.             
  484.         /* add prop obj spec to the event */
  485.         
  486.         if (myErr==noErr)
  487.             myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &propObjSpec);
  488.         
  489.         /* add prop data to the event */
  490.         
  491.         if (myErr==noErr)
  492.             myErr = AEPutParamDesc(&myAppleEvent,keyAEData, theData);
  493.         
  494.         /* send event */
  495.         
  496.         if (myErr==noErr)
  497.             myErr = AESend(&myAppleEvent,
  498.                                          &defReply,
  499.                                          kAENoReply+kAEAlwaysInteract,
  500.                                          kAENormalPriority,
  501.                                          kAEDefaultTimeout,
  502.                                          nil,
  503.                                          nil);
  504.         
  505.         if (myAppleEvent.dataHandle)
  506.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  507.             
  508.         if (&propObjSpec.dataHandle)
  509.           ignoreErr = AEDisposeDesc(&propObjSpec);
  510.         
  511.         if (theData->dataHandle)
  512.             ignoreErr = AEDisposeDesc(theData);
  513.         
  514.         if (toWhom->dataHandle)
  515.             ignoreErr = AEDisposeDesc(toWhom);
  516.         
  517.         return(myErr);
  518.         
  519.     }    /* SendAESetObjProp */
  520.  
  521. /*----------------------------------------------------------------------------------------------*/
  522. /*
  523.     Private AEObject definitions
  524. */
  525. #pragma segment AECommandHandlers
  526.  
  527. #define typeMyAppl       'BAPP' /* sig of my private token type for the app     - appToken   */
  528. #define typeMyWndw             'BWIN'    /* sig of my private token type for windows     - windowToken   */
  529. #define typeMyText           'BTXT'    /* sig of my private token type for text        - textToken     */
  530. #define typeMyTextProp   'BPRP'    /* sig of my private token type for text properties    - textPropToken */
  531. #define typeMyWindowProp 'WPRP'    /* sig of my private token type for window properties  - windowPropToken */
  532. #define typeMyApplProp   'APRP'    /* sig of my private token type for appl properties    - applPropToken */
  533. #define typeMyMenu       'MTKN' /* sig of my private token type for menus       - menuToken  */
  534. #define typeMyMenuItem   'ITKN' /* sig of my private token type for menus       - menuItemToken  */
  535. #define typeMyMenuProp   'MPRP' /* sig of my private token type for menu properties - menuPropToken  */
  536. #define typeMyItemProp   'IPRP' /* sig of my private token type for menu item properties  - menuItemPropToken  */
  537.     
  538. /* These are entirely private to our app - used only when resolving the object specifier */
  539.     
  540. typedef    ProcessSerialNumber appToken;
  541.     
  542. struct applPropToken{
  543.     appToken tokenApplToken;
  544.     DescType tokenApplProperty;
  545. };
  546.  
  547. typedef struct applPropToken applPropToken;
  548.                                         
  549. typedef    WindowPtr WindowToken;
  550.     
  551. struct windowPropToken{
  552.         WindowToken tokenWindowToken;
  553.         DescType    tokenProperty;
  554.     };
  555.     
  556. typedef struct windowPropToken windowPropToken;
  557.     
  558. struct TextToken{
  559.         WindowPtr tokenWindow;
  560.         short     tokenOffset;
  561.         short     tokenLength;
  562.     };
  563.                     
  564. typedef struct TextToken TextToken;
  565.  
  566. struct textPropToken{
  567.         TextToken propertyTextToken;
  568.         DescType  propertyProperty;
  569.     };
  570.                 
  571. typedef struct textPropToken textPropToken;                
  572.                                 
  573. /* Tokens related to menus */
  574.  
  575. struct MenuToken {
  576.     MenuHandle theTokenMenu;
  577.     short      theTokenID;
  578. };
  579.  
  580. typedef struct MenuToken MenuToken;
  581.  
  582. struct MenuItemToken {
  583.     MenuToken  theMenuToken;
  584.     short      theTokenItem;
  585. };
  586.  
  587. typedef struct MenuItemToken MenuItemToken;
  588.  
  589. struct MenuPropToken {
  590.     MenuToken  theMenuToken;
  591.     DescType   theMenuProp;
  592. };
  593.  
  594. typedef struct MenuPropToken MenuPropToken;
  595.  
  596. struct MenuItemPropToken {
  597.     MenuItemToken  theItemToken;
  598.     DescType       theItemProp;
  599. };
  600.  
  601. typedef struct MenuItemPropToken MenuItemPropToken;
  602.  
  603. /*
  604.     Name: GotRequiredParams
  605.     Function: Checks all parameters defined as 'required' have been read
  606. */                            
  607. pascal OSErr GotRequiredParams(const AppleEvent *theAppleEvent)
  608.     {
  609.       OSErr    myErr;
  610.         DescType returnedType;
  611.         Size     actSize;
  612.         
  613.         /* look for the keyMissedKeywordAttr, just to see if it's there */
  614.         
  615.         myErr = AEGetAttributePtr(theAppleEvent,
  616.                                                             keyMissedKeywordAttr,
  617.                                                             typeWildCard,
  618.                                                             &returnedType,
  619.                                                             nil,
  620.                                                             0,
  621.                                                             &actSize);
  622.         
  623.         if (myErr == errAEDescNotFound) 
  624.             return(noErr);            /* attribute not there means we got all req params */
  625.         else 
  626.             if (myErr == noErr)  
  627.                 return(errAEParamMissed);        /* attribute there means missed at least one */
  628.             else 
  629.                 return(myErr);        /* some unexpected arror in looking for the attribute */
  630.     }    /* GotReqiredParams */
  631.  
  632. /**--------------------------------------------------------------------
  633.     Name : SetSelectionOfAppleEventDirectObject
  634.     Function : Resolves the Direct Object into a text token and
  635.                          sets the selection of the specified document to that
  636.                          specified in the direct object.
  637.                          Returns the doc and TEHandle chosen.
  638.     --------------------------------------------------------------------**/
  639.  
  640. pascal OSErr SetSelectionOfAppleEventDirectObject(const AppleEvent *theAppleEvent,
  641.                                                                                                DPtr             *theDocument,
  642.                                                                                                 TEHandle         *theHTE)
  643.     {
  644.         OSErr     myErr;
  645.         DescType  returnedType;
  646.         long      actSize;
  647.         TextToken myTextToken;
  648.         OSErr     paramErr;
  649.         WindowPtr fWin;
  650.                     
  651.         paramErr = AEGetParamPtr( theAppleEvent,
  652.                                                             keyDirectObject,
  653.                                                             typeMyText,
  654.                                                             &returnedType,
  655.                                                       (Ptr)&myTextToken,
  656.                                                             sizeof(myTextToken),
  657.                                                             &actSize);
  658.               
  659.         myErr = GotRequiredParams(theAppleEvent);
  660.         
  661.         /* now let's work on the direct object, if any */
  662.         
  663.         if (paramErr == errAEDescNotFound) 
  664.             {
  665.                 /* no direct object; check we have a window */
  666.                 
  667.                 fWin = FrontWindow();
  668.  
  669.                 if (fWin == nil) 
  670.                     return(-1700); /* Generic Err */
  671.                     
  672.                 *theDocument = DPtrFromWindowPtr(fWin);
  673.                 *theHTE      = (*theDocument)->theText;
  674.             }
  675.         
  676.         if (paramErr == noErr) 
  677.             {
  678.                 /* got a text token */
  679.                 
  680.                 *theDocument = DPtrFromWindowPtr(myTextToken.tokenWindow);
  681.                 *theHTE      = (*theDocument)->theText;
  682.                 
  683.                 TESetSelect(myTextToken.tokenOffset-1,
  684.                             myTextToken.tokenOffset+myTextToken.tokenLength-1,
  685.                                         *theHTE);
  686.                 
  687.             }
  688.             
  689.         if ((paramErr!=noErr) &&
  690.             (paramErr!=errAEDescNotFound))
  691.              {
  692.                  *theDocument = DPtrFromWindowPtr(FrontWindow());
  693.                  *theHTE      = (*theDocument)->theText;
  694.              }
  695.                  
  696.         return(myErr);
  697.              
  698.     } /* SetSelectionOfAppleEventDirectObject */
  699.     
  700. /**--------------------------------------------------------------------
  701.     Name             : SetSelectionOfAppleEventObject
  702.     Function     : Resolves the whatObject type of the AppleEvent into a text
  703.                              token and sets the selection to be that specified in the
  704.                              text token.
  705.                              Returns the doc and TEHandle chosen.
  706.     --------------------------------------------------------------------**/
  707.  
  708. pascal OSErr SetSelectionOfAppleEventObject(OSType            whatObject,
  709.                                                                                     const AppleEvent *theAppleEvent,
  710.                                                                                      DPtr             *theDocument,
  711.                                                                                     TEHandle         *theHTE)
  712.     {
  713.         DescType   returnedType;
  714.         long       actSize;
  715.         TextToken  myTextToken;
  716.         OSErr      paramErr;
  717.             
  718.         paramErr  = AEGetParamPtr(theAppleEvent,
  719.                                                             whatObject,
  720.                                                             typeMyText,
  721.                                                             &returnedType,
  722.                                                       (Ptr)&myTextToken,
  723.                                                             sizeof(myTextToken),
  724.                                                             &actSize);
  725.                                       
  726.         if (paramErr == noErr) 
  727.             {
  728.                 /* got a text token */
  729.                 
  730.                 *theDocument = DPtrFromWindowPtr(myTextToken.tokenWindow);
  731.                 *theHTE      = (*theDocument)->theText;
  732.                 
  733.                 TESetSelect(myTextToken.tokenOffset-1,
  734.                             myTextToken.tokenOffset+myTextToken.tokenLength-1,
  735.                                         *theHTE);
  736.             }
  737.                              
  738.         return(paramErr);
  739.                  
  740.     } /* SetSelectionOfAppleEventObject */
  741.  
  742. /* -----------------------------------------------------------------------
  743.         Name:             DoCutEdit
  744.         Purpose:        Performs a cut text operation on the current text selection
  745.      -----------------------------------------------------------------------**/
  746.     
  747. pascal OSErr DoCutEdit(const AppleEvent *theAppleEvent, AppleEvent *reply ,long refCon)
  748.     {
  749. #pragma unused (reply,refCon)
  750.     
  751.       OSErr    myErr;
  752.       TEHandle theHTE;
  753.         DPtr     theDocument;
  754.     
  755.         myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent,&theDocument,&theHTE);
  756.         
  757.         if (myErr==noErr) 
  758.             {
  759.               DoTECutSectionRecalc(theDocument);
  760.             
  761.                 myErr = (OSErr) ZeroScrap();
  762.                 TECut(theHTE);
  763.                 AdjustScrollbars(theDocument, false);
  764.                 DrawPageExtras(theDocument);
  765.                 theDocument->dirty = true;
  766.             }
  767.             
  768.         return(myErr);
  769.     } /* DoCutEdit */
  770.     
  771. /* -----------------------------------------------------------------------
  772.         Name:             DoCopyEdit
  773.         Purpose:        Performs a copy text operation on the text selection specified
  774.                                 by the appleEvent direct object (if any)
  775.      -----------------------------------------------------------------------**/
  776.     
  777. pascal OSErr DoCopyEdit(const AppleEvent *theAppleEvent,AppleEvent *reply, long refCon)
  778.     {
  779. #pragma unused (reply,refCon)
  780.     
  781.       OSErr    myErr;
  782.         TEHandle theHTE;
  783.         DPtr     theDocument;
  784.  
  785.         /*
  786.                 Here we extract the information about what to copy from the
  787.                 directObject - if any
  788.         */
  789.         
  790.         myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent,&theDocument,&theHTE);
  791.         
  792.         if (myErr==noErr) 
  793.             {
  794.                 myErr = (OSErr) ZeroScrap();
  795.                 TECopy(theHTE);     
  796.             }
  797.             
  798.         if (myErr==noErr) 
  799.             if (SetSelectionOfAppleEventObject(keyAEContainer, 
  800.                                                                                  theAppleEvent,
  801.                                                                                  &theDocument,
  802.                                                                                  &theHTE) == noErr) 
  803.                 {
  804.                     DoTEPasteSectionRecalc(theDocument);
  805.                     
  806.                     TEStylPaste(theHTE);
  807.                     
  808.                     AdjustScrollbars(theDocument, false);
  809.                     
  810.                     DrawPageExtras(theDocument);
  811.                     
  812.                     theDocument->dirty = true;
  813.                 }
  814.             
  815.         return(myErr);
  816.     } /* DoCopyEdit */
  817.     
  818. /* -----------------------------------------------------------------------
  819.         Name:             DoPasteEdit
  820.         Purpose:        Performs a paste text operation on the text selection specified
  821.                                 by the appleEvent direct object (if any)
  822.      -----------------------------------------------------------------------**/
  823.     
  824. pascal OSErr DoPasteEdit(const AppleEvent *theAppleEvent,AppleEvent *reply, long refCon)
  825.     {
  826. #pragma unused (reply,refCon)
  827.     
  828.       OSErr    myErr;
  829.         TEHandle theHTE;
  830.         DPtr     theDocument;
  831.  
  832.         myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent, &theDocument, &theHTE);
  833.         
  834.         if (myErr==noErr) 
  835.             {
  836.                 DoTEPasteSectionRecalc(theDocument);
  837.                 
  838.                 TEStylPaste(theHTE);     
  839.  
  840.                 AdjustScrollbars(theDocument, false);
  841.                 
  842.                 DrawPageExtras(theDocument);
  843.  
  844.                 theDocument->dirty = true;
  845.             }
  846.             
  847.         return(myErr);
  848.     } /* DoPasteEdit */
  849.     
  850. /* -----------------------------------------------------------------------
  851.         Name:             DoDeleteEdit
  852.         Purpose:        Performs a delete text operation on the selection specified
  853.                                 by the appleEvent direct object (if any)
  854.      -----------------------------------------------------------------------**/
  855.      
  856. pascal OSErr DoDeleteEdit(const AppleEvent *theAppleEvent,AppleEvent *reply, long refcon)
  857.     {
  858. #pragma unused (reply,refcon)
  859.     
  860.         OSErr     myErr;
  861.         TEHandle theHTE;
  862.         DPtr     theDocument;
  863.             
  864.         myErr = SetSelectionOfAppleEventDirectObject(theAppleEvent, &theDocument, &theHTE);
  865.         
  866.         if (myErr==noErr)
  867.             {
  868.                 DoTEDeleteSectionRecalc(theDocument);
  869.                         
  870.                 TEDelete(theHTE);     
  871.                 
  872.                 theDocument->dirty = true;
  873.                 AdjustScrollbars(theDocument, false);
  874.                 DrawPageExtras(theDocument);
  875.             }
  876.                     
  877.         return(myErr);
  878.     } /*DoDeleteEdit*/
  879.     
  880. /* -----------------------------------------------------------------------
  881.         Name:             SetTheFontOfTheTokenText
  882.         Purpose:        Sets the font of the text specified by theTextToken to 
  883.                                 the font in name.
  884.      -----------------------------------------------------------------------**/
  885.      
  886. pascal void SetTheFontOfTheTokenText(TextToken theTextToken, Str255 name)
  887.     {
  888.       DPtr      theDocument;
  889.         short     theNumber;
  890.         short     theItem;
  891.         GrafPtr   oldPort;
  892.         TextStyle newStyle;
  893.     
  894.         GetPort(&oldPort);
  895.         
  896.         theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  897.                 
  898.         GetFNum(name, &theNumber);
  899.         
  900.         theItem = ItemForNamedFont(name); /* returns 0 if failed - i.e. SystemFont */
  901.         
  902.         if (gFontMItem)
  903.             CheckItem(myMenus[fontM], gFontMItem, false);
  904.         
  905.         gFontMItem = theItem;
  906.         CheckItem(myMenus[fontM], gFontMItem, true);
  907.         
  908.         theDocument->theFont = theNumber;
  909.         
  910.         TESetSelect(theTextToken.tokenOffset-1,
  911.                                 theTextToken.tokenOffset+theTextToken.tokenLength-1,
  912.                                 theDocument->theText);
  913.         
  914.         newStyle.tsFont = theNumber;
  915.         TESetStyle(doFont, &newStyle, true, theDocument->theText); 
  916.         
  917.         AdjustScrollbars(theDocument, false);
  918.         DrawPageExtras(theDocument);
  919.         theDocument->dirty = true;
  920.         
  921.         SetPort(oldPort);
  922.     } /* SetTheFontOfTheTokenText */
  923.     
  924. /* -----------------------------------------------------------------------
  925.         Name:             SetTheSizeOfTheTokenText
  926.         Purpose:        Sets the size of the text specified by theTextToken to 
  927.                                 the size in theSize.
  928.      -----------------------------------------------------------------------**/
  929.      
  930. pascal void SetTheSizeOfTheTokenText(TextToken theTextToken, short theSize)
  931.     {
  932.         DPtr      theDocument;
  933.         GrafPtr   oldPort;
  934.         TextStyle newStyle;
  935.     
  936.         GetPort(&oldPort);
  937.         
  938.         theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  939.                                 
  940.         theDocument->theSize = theSize;
  941.         
  942.         TESetSelect(theTextToken.tokenOffset-1,
  943.                                 theTextToken.tokenOffset+theTextToken.tokenLength-1,
  944.                                 theDocument->theText);
  945.         
  946.         newStyle.tsSize = theSize;
  947.         TESetStyle(doSize, &newStyle, true, theDocument->theText); 
  948.         
  949.         AdjustScrollbars(theDocument, false);
  950.         DrawPageExtras(theDocument);
  951.         theDocument->dirty = true;
  952.         
  953.         SetPort(oldPort);
  954.     } /* SetTheSizeOfTheTokenText */
  955.     
  956. /* -----------------------------------------------------------------------
  957.         Name:             SetTheStyleOfTheTokenText
  958.         Purpose:        Sets the style of the text specified by theTextToken to 
  959.                                 the style in theStyle.
  960.      -----------------------------------------------------------------------**/
  961.      
  962. pascal void SetTheStyleOfTheTokenText(const TextToken *theTextToken, Style onStyle, Style offStyle)
  963.     {
  964.       DPtr      theDocument;
  965.         GrafPtr   oldPort;
  966.         TextStyle newStyle;
  967.         short     mode;
  968.         Boolean   wasContinuous;
  969.     
  970.         GetPort(&oldPort);
  971.         
  972.         theDocument = DPtrFromWindowPtr(theTextToken->tokenWindow);
  973.                                 
  974.         theDocument->theStyle = onStyle;
  975.         TESetSelect(theTextToken->tokenOffset-1,
  976.                                 theTextToken->tokenOffset+theTextToken->tokenLength-1,
  977.                                 theDocument->theText);
  978.         
  979.         // Check to see if off styles are on for whole selection
  980.         mode = doFace;
  981.         
  982.         wasContinuous = TEContinuousStyle(&mode, &newStyle, theDocument->theText);
  983.         if ((newStyle.tsFace & offStyle) != offStyle) // not off styles are on for all
  984.             {
  985.               // switch on across board so that toggle off will clear all
  986.             newStyle.tsFace  = offStyle - (newStyle.tsFace & offStyle);
  987.                 TESetStyle(doFace+doToggle, &newStyle, false, theDocument->theText); 
  988.             }
  989.             
  990.         newStyle.tsFace  = offStyle;
  991.         TESetStyle(doFace+doToggle, &newStyle,(onStyle==0), theDocument->theText); // toggle all to off
  992.         
  993.         mode = doFace;
  994.         if (onStyle)
  995.             {
  996.                 wasContinuous = TEContinuousStyle(&mode, &newStyle, theDocument->theText);
  997.                 if ((newStyle.tsFace & onStyle) != onStyle) // are they on for only a few chars
  998.                     { 
  999.                         // Need to make all chars have these characteristics
  1000.                     
  1001.                         newStyle.tsFace = onStyle - (newStyle.tsFace & onStyle); // take out those continuous
  1002.                         TESetStyle(doFace+doToggle, &newStyle, true, theDocument->theText);
  1003.                     }
  1004.                 else
  1005.                     TESetStyle(0, &newStyle, true, theDocument->theText); // Just Draw it, no changes
  1006.             }
  1007.             
  1008.         AdjustScrollbars(theDocument, false);
  1009.         DrawPageExtras(theDocument);
  1010.         theDocument->dirty = true;
  1011.         
  1012.         SetPort(oldPort);
  1013.     } /* SetTheStyleOfTheTokenText */
  1014.  
  1015. /* -----------------------------------------------------------------------
  1016.         Name:             SetWindowProperty
  1017.         Purpose:        Sets the window property specified in theWindowPropToken to
  1018.                                 be that supplied in dataDesc.
  1019.      -----------------------------------------------------------------------**/
  1020.      
  1021. pascal OSErr SetWindowProperty(const AEDesc *theWPTokenDesc, const AEDesc *dataDesc)
  1022.     {
  1023.       Str255          theNewName;
  1024.         DPtr            theDocument;
  1025.         OSErr           err;
  1026.         OSErr           ignoreErr;
  1027.         Rect            thePosnRect;
  1028.         Boolean         theBoolean;
  1029.         TEHandle        theHTE;
  1030.         GrafPtr         oldPort;
  1031.         Point           thePosn;
  1032.         THPrint         theTHPrint;
  1033.         windowPropToken theWindowPropToken;
  1034.         AEDesc          newDesc;
  1035.         AEDesc          tokenDesc;
  1036.         Size            tokenSize;
  1037.         TextToken       myTextToken;
  1038.         short           hOffset;
  1039.         short           vOffset;
  1040.     
  1041.         err = AECoerceDesc(theWPTokenDesc, typeMyWindowProp, &newDesc);
  1042.         
  1043.         if (err)
  1044.             return(err);
  1045.  
  1046.         GetRawDataFromDescriptor(&newDesc,
  1047.                                                          (Ptr)&theWindowPropToken,
  1048.                                                          sizeof(theWindowPropToken),
  1049.                                                          &tokenSize);
  1050.         
  1051.         err = AEDisposeDesc(&newDesc);
  1052.     
  1053.         GetPort(&oldPort);
  1054.         SetPort(theWindowPropToken.tokenWindowToken);
  1055.         
  1056.         theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1057.         
  1058.         if (theWindowPropToken.tokenProperty == pName) 
  1059.             {
  1060.                 err = GetPStringFromDescriptor(dataDesc, (char *)theNewName);
  1061.                 if (err==noErr) 
  1062.                     if (theNewName[0] == 0) 
  1063.                         err = errAEWrongDataType;
  1064.                     else
  1065.                         {
  1066.                             SetWTitle(theWindowPropToken.tokenWindowToken, theNewName);
  1067.                             PLstrcpy(theDocument->theFileName, theNewName); /* Should we do this??? */
  1068.                             theDocument->dirty = true;
  1069.                         }
  1070.             }
  1071.             
  1072.         if (theWindowPropToken.tokenProperty == pText) 
  1073.             {
  1074.                 theHTE = theDocument->theText;
  1075.                 TESetSelect(0, 32000, theHTE);
  1076.                 
  1077.                 DoTEDeleteSectionRecalc(theDocument);
  1078.                 TEDelete(theHTE);
  1079.                 
  1080.                 err = GetStyledTextFromDescIntoTEHandle(dataDesc, theHTE);
  1081.  
  1082.                 theDocument->dirty = true;
  1083.             }
  1084.             
  1085.         if (theWindowPropToken.tokenProperty == pBounds) 
  1086.             {
  1087.                 err = GetRectFromDescriptor(dataDesc, &thePosnRect);
  1088.                 /* the rectangle is for the structure region, and is in global coordinates */
  1089.                 /* MoveWindow and SizeWindow apply to the content region, so we have to massage a little */
  1090.  
  1091.                 thePosnRect.top    += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.top -
  1092.                                       (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.top;
  1093.                                                             
  1094.                 thePosnRect.left   += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.left -
  1095.                                       (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.left;
  1096.                                                             
  1097.                 thePosnRect.bottom += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.bottom -
  1098.                                       (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.bottom;
  1099.                                                             
  1100.                 thePosnRect.right  += (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.right -
  1101.                                       (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.right;
  1102.                 
  1103.                 if (EmptyRect(&thePosnRect)) 
  1104.                     err = errAECorruptData;
  1105.                 else
  1106.                     {
  1107.                         MoveWindow(theWindowPropToken.tokenWindowToken,
  1108.                                              thePosnRect.left,
  1109.                                              thePosnRect.top,
  1110.                                              false);
  1111.                         SizeWindow(theWindowPropToken.tokenWindowToken,
  1112.                                              thePosnRect.right- thePosnRect.left,
  1113.                                              thePosnRect.bottom-thePosnRect.top,
  1114.                                              true);
  1115.                         ResizeWindow(theDocument);
  1116.                     }
  1117.             }
  1118.  
  1119.         if (theWindowPropToken.tokenProperty == pPosition) 
  1120.             {
  1121.                 err = GetPointFromDescriptor(dataDesc, &thePosn);
  1122.                 /* the point is for the structure region, and is in global coordinates */
  1123.                 /* MoveWindow applies to the content region, so we have to massage a little */
  1124.         
  1125.                 hOffset = (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.left -
  1126.                           (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.left;
  1127.                                     
  1128.                 vOffset = (**((WindowPeek)theWindowPropToken.tokenWindowToken)->contRgn).rgnBBox.top -
  1129.                           (**((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn).rgnBBox.top;
  1130.                                     
  1131.                                     
  1132.                 thePosn.v  += vOffset;
  1133.                 thePosn.h  += hOffset;
  1134.                 
  1135.                 MoveWindow(theWindowPropToken.tokenWindowToken,
  1136.                                      thePosn.h,
  1137.                                      thePosn.v,
  1138.                                      false);
  1139.                                      
  1140.                 ResizeWindow(theDocument);
  1141.             }
  1142.  
  1143.         if (theWindowPropToken.tokenProperty == pIsZoomed) 
  1144.             {
  1145.                 err = GetBooleanFromDescriptor(dataDesc, &theBoolean);
  1146.                 if (theBoolean)
  1147.                     ZoomWindow(qd.thePort,
  1148.                                          inZoomOut,
  1149.                                          false);
  1150.                 else
  1151.                     ZoomWindow(qd.thePort,
  1152.                                          inZoomIn,
  1153.                                          false);
  1154.                                                          
  1155.                 ResizeWindow(theDocument);
  1156.             }
  1157.             
  1158.         if (theWindowPropToken.tokenProperty == pVisible) 
  1159.             {
  1160.                 err = GetBooleanFromDescriptor(dataDesc, &theBoolean);
  1161.                 if (theBoolean)
  1162.                     ShowWindow(theWindowPropToken.tokenWindowToken);
  1163.                 else
  1164.                     HideWindow(theWindowPropToken.tokenWindowToken);
  1165.             }
  1166.             
  1167.         if (theWindowPropToken.tokenProperty == pPageSetup) 
  1168.             {
  1169.                 err = GetTHPrintFromDescriptor(dataDesc, &theTHPrint);
  1170.                     
  1171.                 if (theTHPrint) 
  1172.                     {
  1173.                         if (theDocument->thePrintSetup) 
  1174.                             DisposHandle((Handle)theDocument->thePrintSetup);
  1175.                             
  1176.                         theDocument->thePrintSetup = theTHPrint;
  1177.                         
  1178.                         ResizePageSetupForDocument(theDocument);
  1179.                     }
  1180.                                 
  1181.             }
  1182.             
  1183.         if (theWindowPropToken.tokenProperty == pShowBorders) 
  1184.             {
  1185.                 err = GetBooleanFromDescriptor(dataDesc, &theBoolean);
  1186.                 theDocument->showBorders = theBoolean;
  1187.                 if (theBoolean)
  1188.                     DrawPageExtras(theDocument); /* Does the clipping as well as drawing borders/page breaks */
  1189.                 else
  1190.                     InvalidateDocument(theDocument);
  1191.             }
  1192.             
  1193.         if (theWindowPropToken.tokenProperty == pSelection) 
  1194.             {                    
  1195.                 err = AECoerceDesc(dataDesc, typeMyText, &tokenDesc);
  1196.                 
  1197.                 GetRawDataFromDescriptor(&tokenDesc,
  1198.                                                                  (Ptr)&myTextToken,
  1199.                                                                  sizeof(myTextToken),
  1200.                                                                  &tokenSize);
  1201.                                                                                  
  1202.                 ignoreErr = AEDisposeDesc(&tokenDesc);
  1203.                 
  1204.                 if (err == noErr) 
  1205.                     {
  1206.                         /* got a text token */
  1207.                         
  1208.                         theDocument = DPtrFromWindowPtr(myTextToken.tokenWindow);
  1209.                         theHTE      = theDocument->theText;
  1210.                         
  1211.                         TESetSelect(myTextToken.tokenOffset-1,
  1212.                                                 myTextToken.tokenOffset+myTextToken.tokenLength-1,
  1213.                                                 theHTE);
  1214.                     }
  1215.             }
  1216.             
  1217.         if ((theWindowPropToken.tokenProperty == pIndex)       ||
  1218.               (theWindowPropToken.tokenProperty == pIsModal)     ||
  1219.               (theWindowPropToken.tokenProperty == pIsResizable) ||
  1220.               (theWindowPropToken.tokenProperty == pHasTitleBar) ||
  1221.               (theWindowPropToken.tokenProperty == pHasCloseBox) ||
  1222.               (theWindowPropToken.tokenProperty == pIsFloating)  ||
  1223.               (theWindowPropToken.tokenProperty == pIsZoomable)  ||
  1224.                 (theWindowPropToken.tokenProperty == pIsModified))   
  1225.             err = errAEEventNotHandled; /* We don't allow these to be set */
  1226.         
  1227.         SetPort(oldPort);
  1228.         
  1229.         return(err);
  1230.                 
  1231.     } /* SetWindowProperty */
  1232.     
  1233. /* -----------------------------------------------------------------------
  1234.         Name:             AddDescStyleItem
  1235.         Purpose:        Adds the kAEXXXX style to theStyle.
  1236.      -----------------------------------------------------------------------**/
  1237.      
  1238. pascal void AddDescStyleItem(DescType theDesc, Style *theStyle)
  1239.     {
  1240.         if (theDesc == kAEBold)
  1241.             *theStyle = *theStyle+bold;
  1242.         else
  1243.         if (theDesc == kAEItalic)
  1244.             *theStyle = *theStyle+italic;
  1245.         else
  1246.         if (theDesc == kAEUnderline)
  1247.             *theStyle = *theStyle+underline;
  1248.         else
  1249.         if (theDesc == kAEOutline)
  1250.             *theStyle = *theStyle+outline;
  1251.         else
  1252.         if (theDesc == kAEShadow)
  1253.             *theStyle = *theStyle+shadow;
  1254.         else
  1255.         if (theDesc == kAECondensed)
  1256.             *theStyle = *theStyle+condense;
  1257.         else
  1258.         if (theDesc == kAEExpanded)
  1259.             *theStyle = *theStyle+extend;
  1260.         else
  1261.         if (theDesc == kAEPlain)
  1262.             *theStyle = 0;
  1263.     } /* AddDescStyleItem */
  1264.     
  1265. pascal OSErr MakeStyleFromAEList(const AEDescList *styleList, Style *theStyle, Boolean *hadPlain)
  1266.     {
  1267.         OSErr     myErr;
  1268.         DescType  styleDesc;
  1269.         long      itemsInList;
  1270.         long      actSize;
  1271.         AEKeyword keywd;
  1272.         DescType  typeCode;
  1273.             
  1274.         *hadPlain = false;
  1275.         *theStyle = 0;
  1276.         
  1277.       myErr = AECountItems(styleList, &itemsInList);
  1278.         while (itemsInList>0)
  1279.           if (myErr==noErr)
  1280.                 {
  1281.                     myErr  = AEGetNthPtr(styleList,
  1282.                                                              itemsInList,
  1283.                                                              typeEnumerated,
  1284.                                                              &keywd,
  1285.                                                              &typeCode,
  1286.                                                              (Ptr)&styleDesc,
  1287.                                                              sizeof(styleDesc),
  1288.                                                              &actSize);
  1289.                     
  1290.                     AddDescStyleItem(styleDesc, theStyle);
  1291.                     
  1292.                     if (styleDesc == kAEPlain) 
  1293.                       {
  1294.                             itemsInList = 0;
  1295.                             *hadPlain    = true;
  1296.                         }
  1297.                     else
  1298.                       itemsInList--;
  1299.                 }
  1300.                 
  1301.         return(myErr);
  1302.     } /*MakeStyleFromAEList*/
  1303.     
  1304. pascal OSErr GetTextStyles(const AEDesc *dataDesc, Style *onStyles, Style *offStyles)
  1305.     {
  1306.         OSErr      myErr;
  1307.         OSErr      ignoreErr;
  1308.         AEDescList textSDesc;
  1309.         AEDescList onDesc;
  1310.         AEDescList offDesc;
  1311.         Boolean    hadPlain;
  1312.             
  1313.       textSDesc.dataHandle = nil;
  1314.       onDesc.dataHandle    = nil;
  1315.       offDesc.dataHandle   = nil;
  1316.         
  1317.         *onStyles  = 0;
  1318.         *offStyles = 0;
  1319.         
  1320.       myErr = AECoerceDesc(dataDesc, typeAERecord, &textSDesc);
  1321.         
  1322.         if (myErr==noErr)
  1323.           myErr = AEGetKeyDesc(&textSDesc, keyAEOnStyles, typeAEList, &onDesc);
  1324.         
  1325.         if (myErr==noErr)
  1326.           myErr = AEGetKeyDesc(&textSDesc, keyAEOffStyles, typeAEList, &offDesc);
  1327.         
  1328.         if (myErr==noErr)
  1329.             myErr = MakeStyleFromAEList(&onDesc,  onStyles, &hadPlain);
  1330.         
  1331.         if (hadPlain)
  1332.             *offStyles = bold+italic+underline+outline+shadow+condense+extend;
  1333.         else
  1334.             {
  1335.                 if (myErr==noErr)
  1336.                     myErr = MakeStyleFromAEList(&offDesc, offStyles, &hadPlain);
  1337.                 
  1338.                 if (hadPlain)
  1339.                     myErr = errAEEventFailed;
  1340.             }
  1341.             
  1342.         if (textSDesc.dataHandle)
  1343.             ignoreErr = AEDisposeDesc(&textSDesc);
  1344.             
  1345.         if (onDesc.dataHandle)
  1346.             ignoreErr = AEDisposeDesc(&onDesc);
  1347.             
  1348.         if (offDesc.dataHandle)
  1349.             ignoreErr = AEDisposeDesc(&offDesc);
  1350.         
  1351.         return(myErr);
  1352.     } /* GetTextStyles */
  1353.  
  1354. /* -----------------------------------------------------------------------
  1355.         Name:             SetTextProperty
  1356.         Purpose:        Sets the text property specfied by theTextPropToken to
  1357.                                 that in dataDesc.
  1358.      -----------------------------------------------------------------------**/
  1359.      
  1360. pascal OSErr SetTextProperty(const AEDesc *tokenDesc, const AEDesc *dataDesc)
  1361.     {
  1362.       TEHandle      theHTE;
  1363.         DPtr          theDoc;
  1364.         Str255        name;
  1365.         short         theSize;
  1366.         OSErr         myErr;
  1367.         Style         onStyle;
  1368.         Style         offStyle;
  1369.         textPropToken theTextPropToken;
  1370.         AEDesc        newDesc;
  1371.         Size          tokenSize;
  1372.         
  1373.       newDesc.dataHandle = nil;
  1374.         
  1375.       myErr = AECoerceDesc(tokenDesc, typeMyTextProp, &newDesc);
  1376.         if (myErr == noErr)
  1377.             {
  1378.                 GetRawDataFromDescriptor(&newDesc,
  1379.                                                                  (Ptr)&theTextPropToken,
  1380.                                                                  sizeof(theTextPropToken),
  1381.                                                                  &tokenSize);
  1382.                 myErr = AEDisposeDesc(&newDesc);
  1383.             }
  1384.         else
  1385.             return(myErr);
  1386.             
  1387.         theDoc = DPtrFromWindowPtr(theTextPropToken.propertyTextToken.tokenWindow);
  1388.         theDoc->dirty = true;
  1389.         
  1390.         if (theTextPropToken.propertyProperty == pText) 
  1391.             {
  1392.                 theHTE = theDoc->theText;
  1393.                 TESetSelect(theTextPropToken.propertyTextToken.tokenOffset-1,
  1394.                                         theTextPropToken.propertyTextToken.tokenOffset+
  1395.                                                                 theTextPropToken.propertyTextToken.tokenLength-1,
  1396.                                         theHTE);
  1397.                 
  1398.                 DoTEDeleteSectionRecalc(theDoc);
  1399.                 TEDelete(theHTE);
  1400.                 
  1401.                 myErr = GetStyledTextFromDescIntoTEHandle(dataDesc, theHTE);
  1402.  
  1403.                 return(myErr);
  1404.             }
  1405.             
  1406.         if (theTextPropToken.propertyProperty == pFont) 
  1407.             {                    
  1408.                 myErr = GetPStringFromDescriptor(dataDesc, (char *)name);
  1409.                 
  1410.                 SetTheFontOfTheTokenText(theTextPropToken.propertyTextToken,
  1411.                                                                  name);
  1412.                 
  1413.                 return(noErr);
  1414.             }
  1415.             
  1416.         if (theTextPropToken.propertyProperty == pPointSize) 
  1417.             {
  1418.                 myErr = GetIntegerFromDescriptor(dataDesc, &theSize);
  1419.                 SetTheSizeOfTheTokenText(theTextPropToken.propertyTextToken,
  1420.                                                                  theSize);
  1421.                 
  1422.                 return(noErr);
  1423.             }
  1424.             
  1425.         if (theTextPropToken.propertyProperty == pTextStyles) 
  1426.             {
  1427.                 onStyle  = 0;
  1428.                 offStyle = 0;
  1429.                 
  1430.                 myErr = GetTextStyles(dataDesc, &onStyle, &offStyle);
  1431.  
  1432.         if (onStyle & offStyle != 0)
  1433.                     myErr = errAEEventFailed;
  1434.                 else
  1435.                     SetTheStyleOfTheTokenText(&theTextPropToken.propertyTextToken,
  1436.                                                                         onStyle,
  1437.                                                                         offStyle);
  1438.                 
  1439.                 return(myErr);
  1440.             }
  1441.                         
  1442.         return(errAEWrongDataType);
  1443.             
  1444.     } /* SetTextProperty */
  1445.     
  1446. /* -----------------------------------------------------------------------
  1447.         Name:             HandleSetData
  1448.         Purpose:        Resolves the object into a token (could be one of many) and
  1449.                                 the sets the data of that object to dataDesc.
  1450.      -----------------------------------------------------------------------**/
  1451.      
  1452. pascal OSErr HandleSetData(const AEDesc *theObj, const AEDesc *dataDesc)
  1453. {
  1454.   OSErr           myErr;
  1455.     AEDesc          newDesc;
  1456.     DPtr            theDocument;
  1457.     TEHandle        theHTE;
  1458.     TextToken       theTextToken;
  1459.     Size            tokenSize;
  1460.     AEDesc          objTokenDesc;
  1461.     OSErr           ignoreErr;
  1462.     
  1463.     objTokenDesc.dataHandle = nil;
  1464.     newDesc.dataHandle      = nil;
  1465.     
  1466.     /*
  1467.         Coerce theObj into a token which we can use - 
  1468.              set the property or data for that token
  1469.     */
  1470.     
  1471.     myErr = AEResolve(theObj ,kAEIDoMinimum, &objTokenDesc);
  1472.         
  1473.     /* We don't actually allow ANY app property setting, but
  1474.        just incase we'll decode looking for an typeMyApplProp and flag an error -
  1475.          do same for menu related tokens
  1476.     */
  1477.     
  1478.     if ((objTokenDesc.descriptorType == typeMyApplProp) ||
  1479.             (objTokenDesc.descriptorType == typeMyMenu    ) ||
  1480.             (objTokenDesc.descriptorType == typeMyMenuProp) ||
  1481.             (objTokenDesc.descriptorType == typeMyMenuItem) ||
  1482.             (objTokenDesc.descriptorType == typeMyItemProp))
  1483.         myErr = errAEWrongDataType;
  1484.     else
  1485.     
  1486.     if (objTokenDesc.descriptorType == typeMyWindowProp)
  1487.         myErr = SetWindowProperty(&objTokenDesc, dataDesc);
  1488.     else
  1489.     
  1490.     if (objTokenDesc.descriptorType == typeMyTextProp)
  1491.         myErr = SetTextProperty(&objTokenDesc, dataDesc);
  1492.     else
  1493.  
  1494.     if (objTokenDesc.descriptorType == typeMyText)
  1495.       if (AECoerceDesc(&objTokenDesc, typeMyText, &newDesc) == noErr) 
  1496.             {
  1497.                 GetRawDataFromDescriptor(&newDesc,
  1498.                                                                  (Ptr)&theTextToken,
  1499.                                                                  sizeof(theTextToken),
  1500.                                                                  &tokenSize);
  1501.                                                                  
  1502.                 myErr = AEDisposeDesc(&newDesc);
  1503.                                                                  
  1504.                 theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  1505.                 theHTE            = theDocument->theText;
  1506.                 
  1507.                 TESetSelect(theTextToken.tokenOffset-1,
  1508.                                         theTextToken.tokenOffset+
  1509.                                                                 theTextToken.tokenLength-1,
  1510.                                         theHTE);
  1511.                 
  1512.                 DoTEDeleteSectionRecalc(theDocument);
  1513.                 TEDelete(theHTE);
  1514.                 
  1515.                 myErr = GetStyledTextFromDescIntoTEHandle(dataDesc, theHTE);
  1516.                 
  1517.                 theDocument->dirty = true;
  1518.             }
  1519.         
  1520.     ignoreErr = AEDisposeDesc(&objTokenDesc);
  1521.  
  1522.     return(myErr);
  1523. }    /* HandleSetData */
  1524.  
  1525. /*
  1526.     A few convenient FORWARDS...
  1527. */
  1528.  
  1529. pascal OSErr MakeWindowObj( WindowPtr theWindow, AEDesc *dMyDoc);
  1530.  
  1531. /*
  1532.     Back to real code
  1533. */
  1534. pascal OSErr MakeSelTextObj( WindowPtr theWindow,
  1535.                                                          TEHandle  theTextEditHandle,
  1536.                                                          AEDesc    *selTextObj)
  1537. /*
  1538.     This is a hack to get the AppleScript Alpha to work...
  1539. */
  1540.     {
  1541.         OSErr    myErr;
  1542.         OSErr    ignoreErr;
  1543.         AEDesc   dNull;
  1544.         AEDesc   dMyDoc;
  1545.         AEDesc   startOfs;
  1546.         AEDesc   endOfs;
  1547.         AEDesc   startObj;
  1548.         AEDesc   endObj;
  1549.         AEDesc   rangeDesc;
  1550.         long     startChar;
  1551.         long     endChar;
  1552.         Boolean  spotFlag;
  1553.             
  1554.         myErr = noErr;
  1555.         
  1556.         if (theWindow==nil) 
  1557.             return(noErr);
  1558.             
  1559.         selTextObj->dataHandle = nil;
  1560.         dMyDoc.dataHandle      = nil;
  1561.         startObj.dataHandle    = nil;
  1562.         endObj.dataHandle      = nil;
  1563.         
  1564.         /* 
  1565.             make the window object 
  1566.         */
  1567.         
  1568.         myErr = MakeWindowObj(theWindow, &dMyDoc);
  1569.             
  1570.         if (myErr==noErr)
  1571.             {
  1572.                 /* get the start and end of selection */
  1573.                 
  1574.                 startChar = (*theTextEditHandle)->selStart+1;    /* start counting obj's from 1, not 0 */
  1575.                 endChar   = (*theTextEditHandle)->selEnd;
  1576.                 spotFlag  = ((*theTextEditHandle)->selStart == (*theTextEditHandle)->selEnd);
  1577.                     
  1578.                 myErr = CreateOffsetDescriptor(startChar, &startOfs);
  1579.                             
  1580.                 if (myErr==noErr)
  1581.                     if (spotFlag)
  1582.                         myErr = CreateObjSpecifier(cSpot,
  1583.                                                                              &dMyDoc,
  1584.                                                                              formAbsolutePosition, 
  1585.                                                                              &startOfs, 
  1586.                                                                              true, 
  1587.                                                                              selTextObj);
  1588.                     else
  1589.                         {
  1590.                             /* not a spot - must represent as range */
  1591.                             /* make obj for start char */
  1592.                             
  1593.                             myErr = AECreateDesc(typeNull, nil , 0, &dNull);
  1594.                             
  1595.                             myErr = CreateObjSpecifier(cChar,
  1596.                                                        &dNull,
  1597.                                                                                  formAbsolutePosition,
  1598.                                                                                  &startOfs,
  1599.                                                                                  false,
  1600.                                                                                  &startObj);
  1601.                             
  1602.                             if (myErr==noErr) 
  1603.                                 myErr = CreateOffsetDescriptor(endChar, &endOfs);
  1604.                             
  1605.                             if (myErr==noErr) 
  1606.                                 myErr = CreateObjSpecifier(cChar,
  1607.                                                                                      &dNull,
  1608.                                                                                      formAbsolutePosition,
  1609.                                                                                      &endOfs,
  1610.                                                                                      false,
  1611.                                                                                      &endObj);
  1612.                             
  1613.                             if (myErr==noErr) 
  1614.                                 myErr = CreateRangeDescriptor(&startObj,
  1615.                                                                                             &endObj,
  1616.                                                                                             false,
  1617.                                                                                             &rangeDesc);
  1618.                             
  1619.                             if (myErr==noErr) 
  1620.                                 myErr = CreateObjSpecifier(cChar,
  1621.                                                                                      &dMyDoc,
  1622.                                                                                      formRange,
  1623.                                                                                      &rangeDesc,
  1624.                                                                                      true,
  1625.                                                                                      selTextObj);
  1626.                                                                                      
  1627.                             if (startObj.dataHandle)
  1628.                               ignoreErr = AEDisposeDesc(&startObj);
  1629.                                 
  1630.                             if (startOfs.dataHandle)
  1631.                               ignoreErr = AEDisposeDesc(&startOfs);
  1632.                                 
  1633.                             if (endObj.dataHandle)
  1634.                               ignoreErr = AEDisposeDesc(&endObj);
  1635.                                 
  1636.                             if (endOfs.dataHandle)
  1637.                               ignoreErr = AEDisposeDesc(&endOfs);
  1638.                         }
  1639.             }
  1640.             
  1641.         return(myErr);
  1642.     }    /* MakeSelTextObj */
  1643.  
  1644. /* -----------------------------------------------------------------------
  1645.         Name:             DoSetData
  1646.         Purpose:        Handles the SetData Apple Event, extracting the direct
  1647.                                 object (which says what to set) and the data (what to set
  1648.                                 it to).
  1649.      -----------------------------------------------------------------------**/
  1650.      
  1651. pascal OSErr DoSetData(const AppleEvent *theAppleEvent,
  1652.                                                AppleEvent *reply,
  1653.                                                          long        handlerRefCon)
  1654.   {
  1655. #pragma unused (reply, handlerRefCon)
  1656.     
  1657.         OSErr  myErr;
  1658.         OSErr  ignoreErr;
  1659.         AEDesc myDirObj;
  1660.         AEDesc myDataDesc;
  1661.         
  1662.         myDataDesc.dataHandle = nil;
  1663.         myDirObj.dataHandle   = nil;
  1664.                 
  1665.         /* pick up the direct object, which is the object whose data is to be set */
  1666.         
  1667.         myErr = AEGetParamDesc(theAppleEvent,
  1668.                                                      keyDirectObject,
  1669.                                                      typeWildCard,
  1670.                                                      &myDirObj);
  1671.             
  1672.         /* now the data to set it to - typeWildCard means get as is*/
  1673.         if (myErr == noErr) 
  1674.             myErr = AEGetParamDesc( theAppleEvent,
  1675.                                                             keyAEData,
  1676.                                                             typeWildCard,
  1677.                                                             &myDataDesc);
  1678.         
  1679.         /* missing any parameters? */
  1680.         if (myErr == noErr) 
  1681.             myErr = GotRequiredParams(theAppleEvent);
  1682.         
  1683.         /* set the data */
  1684.         if (myErr == noErr) 
  1685.             myErr = HandleSetData(&myDirObj, &myDataDesc);
  1686.             
  1687.         if (myDataDesc.dataHandle)
  1688.             ignoreErr = AEDisposeDesc(&myDataDesc);
  1689.             
  1690.         if (myDirObj.dataHandle)
  1691.             ignoreErr = AEDisposeDesc(&myDirObj);
  1692.   
  1693.         return(myErr);
  1694.     }    /* DoSetData */
  1695.     
  1696. pascal void StyleTokConst(short theStyleItem, DescType *thekConst)
  1697.     {
  1698.         switch (theStyleItem) {
  1699.           case bold      : *thekConst = kAEBold;
  1700.                                              break;
  1701.       case italic    : *thekConst = kAEItalic;
  1702.                                              break;
  1703.       case underline : *thekConst = kAEUnderline;
  1704.                                              break;
  1705.       case outline   : *thekConst = kAEOutline;
  1706.                                              break;
  1707.             case shadow    : *thekConst = kAEShadow;
  1708.                                              break;
  1709.             case condense  : *thekConst = kAECondensed;
  1710.                                              break;
  1711.             case extend    : *thekConst = kAEExpanded;
  1712.                                              break;
  1713.         }
  1714.     } /*StyleTokConst*/
  1715.  
  1716. pascal OSErr BuildTypeTextStylesDesc(Style onStyles, Style offStyles, AEDesc *resultDesc)
  1717.     {
  1718.  
  1719.         OSErr     myErr;
  1720.         OSErr     ignoreErr;
  1721.         short     myStyleItem;
  1722.         DescType  styleConst;
  1723.         AEDesc    onStylesDesc;
  1724.         AEDesc    offStylesDesc;
  1725.         AEDesc    dataDesc;
  1726.         
  1727.         onStylesDesc.dataHandle  = nil;
  1728.         offStylesDesc.dataHandle = nil;
  1729.         dataDesc.dataHandle  = nil;
  1730.     
  1731.         myErr = AECreateList(nil, 0, true,  &dataDesc);
  1732.         
  1733.         myErr = AECreateList(nil, 0, false, &onStylesDesc);
  1734.         myErr = AECreateList(nil, 0, false, &offStylesDesc);
  1735.         
  1736.         for (myStyleItem = bold; myStyleItem<=extend; myStyleItem = myStyleItem <<1)
  1737.             if (myErr==noErr)
  1738.                 {
  1739.                     StyleTokConst(myStyleItem, &styleConst);
  1740.                     if (myStyleItem & onStyles)
  1741.                         myErr  = AEPutPtr(&onStylesDesc,
  1742.                                                             0,                /*add to end of list*/
  1743.                                                             typeEnumerated, /* text for style name */
  1744.                                                             (Ptr)&styleConst,
  1745.                                                             sizeof(styleConst));
  1746.                     
  1747.                     if (myStyleItem & offStyles)
  1748.                         myErr  = AEPutPtr(&offStylesDesc,
  1749.                                                             0,                /*add to end of list*/
  1750.                                                             typeEnumerated, /* text for style name */
  1751.                                                             (Ptr)&styleConst,
  1752.                                                             sizeof(styleConst));
  1753.             }
  1754.         
  1755.         if (myErr==noErr)
  1756.             myErr = AEPutKeyDesc(&dataDesc, keyAEOnStyles,  &onStylesDesc);
  1757.         
  1758.         if (myErr==noErr)
  1759.             myErr = AEPutKeyDesc(&dataDesc, keyAEOffStyles, &offStylesDesc);
  1760.         
  1761.         if (myErr==noErr)
  1762.             myErr = AECoerceDesc(&dataDesc, typeTextStyles, resultDesc);
  1763.             
  1764.         if (onStylesDesc.dataHandle)
  1765.             ignoreErr = AEDisposeDesc(&onStylesDesc);
  1766.             
  1767.         if (offStylesDesc.dataHandle)
  1768.             ignoreErr = AEDisposeDesc(&offStylesDesc);
  1769.             
  1770.         if (dataDesc.dataHandle)
  1771.             ignoreErr = AEDisposeDesc(&dataDesc);
  1772.     
  1773.         return(myErr);
  1774.     }
  1775.  
  1776. pascal OSErr BuildTextStylesDesc(Style theStyle, AEDesc *resultDesc)
  1777.     {
  1778.         short     myStyleItem;
  1779.         Style     onStyles;
  1780.         Style     offStyles;
  1781.                 
  1782.         onStyles  = 0;
  1783.         offStyles = 0;
  1784.         
  1785.         for (myStyleItem = bold; myStyleItem<=extend; myStyleItem = myStyleItem <<1)
  1786.             {
  1787.                 if (myStyleItem & theStyle)
  1788.                     onStyles  = onStyles  + myStyleItem;
  1789.                 else
  1790.                     offStyles = offStyles + myStyleItem;
  1791.             }
  1792.     
  1793.         return(BuildTypeTextStylesDesc(onStyles, offStyles, resultDesc));
  1794.         
  1795.     } /* BuildTextStylesDesc */
  1796.         
  1797. pascal OSErr BuildStyledTextDesc(TEHandle theHTE, short start, short howLong, AEDesc *resultDesc)
  1798.     {
  1799.         AEDesc       listDesc;
  1800.         short        oldSelStart;
  1801.         short        oldSelEnd;
  1802.         StScrpHandle myStScrpHandle;
  1803.         OSErr        myErr;
  1804.         OSErr        ignoreErr;
  1805.         
  1806.         listDesc.dataHandle = nil;
  1807.         
  1808.         oldSelStart = (**theHTE).selStart;
  1809.         oldSelEnd   = (**theHTE).selEnd;
  1810.         
  1811.         TESetSelect(start-1, start+howLong-2, theHTE);
  1812.         
  1813.         myErr = AECreateList(nil, 0, true,  &listDesc);
  1814.         
  1815.         HLock((Handle)(**theHTE).hText);
  1816.                                                          
  1817.         if (myErr==noErr)
  1818.             myErr = AEPutKeyPtr(&listDesc,
  1819.                                 keyAEText,
  1820.                                                     typeChar,
  1821.                                                     (Ptr)&(*(**theHTE).hText)[start-1],
  1822.                                                     howLong);
  1823.                                                     
  1824.         HUnlock((Handle)(**theHTE).hText);
  1825.         
  1826.         myStScrpHandle = GetStylScrap(theHTE);
  1827.         
  1828.         if (myStScrpHandle)
  1829.             {
  1830.                 HLock((Handle)myStScrpHandle);
  1831.                 
  1832.                 if (myErr==noErr)
  1833.                     myErr = AEPutKeyPtr(&listDesc,
  1834.                                         keyAEStyles,
  1835.                                                             typeScrapStyles,
  1836.                                                             (Ptr)*myStScrpHandle,
  1837.                                                             GetHandleSize((Handle)myStScrpHandle));
  1838.                     
  1839.                 HUnlock((Handle)myStScrpHandle);
  1840.             }    
  1841.         else
  1842.             myErr = AEPutKeyPtr(&listDesc,
  1843.                                                     keyAEStyles,
  1844.                                                     typeScrapStyles,
  1845.                                                     (Ptr)nil,
  1846.                                                     0);
  1847.         
  1848.         if (myErr==noErr)
  1849.             myErr = AECoerceDesc(&listDesc, typeStyledText, resultDesc); // should be typeIntlText
  1850.         
  1851.         if (listDesc.dataHandle)
  1852.             ignoreErr = AEDisposeDesc(&listDesc);
  1853.         
  1854.         TESetSelect(oldSelStart, oldSelEnd, theHTE);
  1855.         
  1856.         return(myErr);
  1857.     }
  1858.         
  1859. /* -----------------------------------------------------------------------
  1860.         Name:             GetTextProperty
  1861.         Purpose:        Fills dataDesc with the requested text property.
  1862.      -----------------------------------------------------------------------**/
  1863.      
  1864. pascal OSErr GetTextProperty(const AEDesc *theTokenDesc, AEDesc *dataDesc)
  1865.     {
  1866.       DPtr          theDocument;
  1867.         TEHandle      theHTE;
  1868.         Str255        fontName;
  1869.         short         theSize;
  1870.         GrafPtr       oldPort;
  1871.         TextStyle     theTextStyle;
  1872.         short         lineHeight;
  1873.         short         fontAscent;
  1874.         textPropToken theTextPropToken;
  1875.         OSErr         myErr;
  1876.         Size          tokenSize;
  1877.         AEDesc        newDesc;
  1878.         
  1879.       myErr = AECoerceDesc(theTokenDesc, typeMyTextProp, &newDesc);
  1880.         if (myErr == noErr)
  1881.             {
  1882.                 GetRawDataFromDescriptor(&newDesc,
  1883.                                                                  (Ptr)&theTextPropToken,
  1884.                                                                  sizeof(theTextPropToken),
  1885.                                                                  &tokenSize);
  1886.                 myErr= AEDisposeDesc(&newDesc);                                                 
  1887.             }
  1888.         else
  1889.           return(myErr);
  1890.  
  1891.         /*
  1892.             For each property we build a descriptor to be returned as the reply.
  1893.         */
  1894.                 
  1895.         theDocument = DPtrFromWindowPtr(theTextPropToken.propertyTextToken.tokenWindow);
  1896.         theHTE             = theDocument->theText;
  1897.         
  1898.         TEGetStyle (theTextPropToken.propertyTextToken.tokenOffset-1,
  1899.                                 &theTextStyle,
  1900.                                 &lineHeight,
  1901.                                 &fontAscent,
  1902.                                 theHTE);
  1903.                                         
  1904.         if (theTextPropToken.propertyProperty == pText) 
  1905.             {
  1906.                 myErr = BuildStyledTextDesc(theHTE,
  1907.                                                                         theTextPropToken.propertyTextToken.tokenOffset,
  1908.                                                                         theTextPropToken.propertyTextToken.tokenLength,
  1909.                                                                         dataDesc);
  1910.             }
  1911.         else
  1912.         if (theTextPropToken.propertyProperty == pFont) 
  1913.             {
  1914.             
  1915.                 GetFontName(theTextStyle.tsFont, fontName);
  1916.                 
  1917.                 myErr = AECreateDesc(typeChar,
  1918.                                                          (Ptr)&fontName[1],
  1919.                                                          fontName[0],
  1920.                                                          dataDesc);
  1921.             }
  1922.             
  1923.         else
  1924.         if (theTextPropToken.propertyProperty == pTextStyles) 
  1925.             myErr = BuildTextStylesDesc(theTextStyle.tsFace, dataDesc);
  1926.  
  1927.         else
  1928.         if (theTextPropToken.propertyProperty == pPointSize) 
  1929.             myErr =CreateOffsetDescriptor(theTextStyle.tsSize, dataDesc);
  1930.  
  1931.         else
  1932.         if (theTextPropToken.propertyProperty == pScriptTag) 
  1933.             myErr = CreateOffsetDescriptor(smSystemScript, dataDesc);
  1934.         else
  1935.         if (theTextPropToken.propertyProperty == pStringWidth) 
  1936.             {
  1937.                 GetPort(&oldPort);
  1938.                 SetPort(theTextPropToken.propertyTextToken.tokenWindow);
  1939.                 
  1940.                 HLock((Handle)(*theHTE)->hText);
  1941.                 theSize = TextWidth(&(*theHTE)->hText,
  1942.                                                         theTextPropToken.propertyTextToken.tokenOffset-1,
  1943.                                                         theTextPropToken.propertyTextToken.tokenLength);
  1944.                 
  1945.                 HUnlock((Handle)(*theHTE)->hText);
  1946.                 
  1947.                 SetPort(oldPort);
  1948.                 return(CreateOffsetDescriptor(theSize, dataDesc));
  1949.             }
  1950.             
  1951.         else
  1952.         if (theTextPropToken.propertyProperty == pColor) 
  1953.             myErr = AECreateDesc(typeRGBColor,
  1954.                                                      (Ptr)&theTextStyle.tsColor,
  1955.                                                      sizeof(theTextStyle.tsColor),
  1956.                                                      dataDesc);
  1957.         else
  1958.             myErr = errAEEventNotHandled;
  1959.             
  1960.         return(myErr);
  1961.         
  1962.     } /*GetTextProperty*/
  1963.     
  1964. /* -----------------------------------------------------------------------
  1965.         Name:             GetWindowProperty
  1966.         Purpose:        Fills dataDesc with the requested window property.
  1967.      -----------------------------------------------------------------------**/
  1968. typedef Rect **RectHandle;
  1969.  
  1970. pascal OSErr GetWindowProperty(const AEDesc *theWPTokenObj, AEDesc *dataDesc)
  1971.   {             
  1972.     OSErr           theErr;
  1973.         Str255          theName;
  1974.         Boolean         theBoolean;
  1975.         Rect            theRect;
  1976.         Point           thePoint;
  1977.         Rect            winRect;
  1978.         Rect            userRect;
  1979.         short           theIndex;
  1980.         DPtr            theDocument;
  1981.         TEHandle        theHTE;
  1982.         windowPropToken theWindowPropToken;
  1983.         AEDesc          newDesc;
  1984.         Size            tokenSize;
  1985.         
  1986.       theErr = AECoerceDesc(theWPTokenObj,typeMyWindowProp, &newDesc);
  1987.         
  1988.         if (theErr == noErr)
  1989.             {
  1990.                 GetRawDataFromDescriptor(&newDesc,
  1991.                                                                  (Ptr)&theWindowPropToken,
  1992.                                                                  sizeof(theWindowPropToken),
  1993.                                                                  &tokenSize);
  1994.                                                                  
  1995.                 theErr = AEDisposeDesc(&newDesc);
  1996.             }
  1997.         else
  1998.           return(theErr);
  1999.             
  2000.         theErr = kAEGenericErr;
  2001.         
  2002.         if (theWindowPropToken.tokenProperty == pName) 
  2003.             {
  2004.                 GetWTitle(theWindowPropToken.tokenWindowToken, theName);
  2005.                 theErr = AECreateDesc(typeChar,
  2006.                                                             (Ptr)&theName[1],
  2007.                                                             theName[0],
  2008.                                                             dataDesc);
  2009.             }
  2010.             
  2011.         if (theWindowPropToken.tokenProperty == pText) 
  2012.             {
  2013.                 theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2014.                 theHTE      = theDocument->theText;
  2015.                 
  2016.                 theErr = BuildStyledTextDesc(theHTE,
  2017.                                                                          1,
  2018.                                                                          (**theHTE).teLength,
  2019.                                                                          dataDesc);
  2020.             }
  2021.             
  2022.         if (theWindowPropToken.tokenProperty == pBounds) 
  2023.             {
  2024.                 SetPort(theWindowPropToken.tokenWindowToken);
  2025.                 
  2026.                 theRect = (*((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn)->rgnBBox;
  2027.                 
  2028.                 theErr  = AECreateDesc(typeQDRectangle,
  2029.                                                              (Ptr)&theRect,
  2030.                                                              sizeof(theRect),
  2031.                                                              dataDesc);
  2032.             }
  2033.             
  2034.         if (theWindowPropToken.tokenProperty == pPosition) 
  2035.             {
  2036.                 thePoint.v = (*((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn)->rgnBBox.top;
  2037.                 thePoint.h = (*((WindowPeek)theWindowPropToken.tokenWindowToken)->strucRgn)->rgnBBox.left;
  2038.                             
  2039.                 theErr   = AECreateDesc(typeQDPoint,
  2040.                                                               (Ptr)&thePoint,
  2041.                                                               sizeof(thePoint),
  2042.                                                               dataDesc);
  2043.             }
  2044.                         
  2045.         if (theWindowPropToken.tokenProperty == pVisible) 
  2046.             {
  2047.                 theBoolean = ((WindowPeek)theWindowPropToken.tokenWindowToken)->visible;
  2048.                                 
  2049.                 theErr  = AECreateDesc(typeBoolean,
  2050.                                                              (Ptr)&theBoolean,
  2051.                                                              sizeof(theBoolean),
  2052.                                                              dataDesc);
  2053.             }
  2054.             
  2055.         if (theWindowPropToken.tokenProperty == pIsModal) 
  2056.             {
  2057.                 theBoolean = false;
  2058.                                 
  2059.                 theErr  = AECreateDesc(typeBoolean,
  2060.                                                              (Ptr)&theBoolean,
  2061.                                                              sizeof(theBoolean),
  2062.                                                              dataDesc);
  2063.             }
  2064.             
  2065.         if (theWindowPropToken.tokenProperty == pShowBorders) 
  2066.             {
  2067.                 theDocument  = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2068.                 theBoolean   = theDocument->showBorders;
  2069.                                 
  2070.                 theErr  = AECreateDesc(typeBoolean,
  2071.                                                              (Ptr)&theBoolean,
  2072.                                                              sizeof(theBoolean),
  2073.                                                              dataDesc);
  2074.             }
  2075.             
  2076.         if (theWindowPropToken.tokenProperty == pIsZoomed) 
  2077.             {
  2078.                 if (((WindowPeek)theWindowPropToken.tokenWindowToken)->spareFlag) 
  2079.                     {
  2080.                         SetPort(theWindowPropToken.tokenWindowToken);
  2081.                         
  2082.                         userRect = **((RectHandle)((WindowPeek)qd.thePort)->dataHandle);
  2083.                         winRect  = qd.thePort->portRect;
  2084.                         LocalToGlobal((Point *)&winRect.top);
  2085.                         LocalToGlobal((Point *)&winRect.bottom);
  2086.                         
  2087.                         theBoolean = !EqualRect(&userRect, &winRect);
  2088.                     }
  2089.                 else
  2090.                     theBoolean = false;
  2091.                                 
  2092.                 theErr  = AECreateDesc(typeBoolean,
  2093.                                                              (Ptr)&theBoolean,
  2094.                                                              sizeof(theBoolean),
  2095.                                                              dataDesc);
  2096.             }
  2097.             
  2098.         if ((theWindowPropToken.tokenProperty == pIsResizable) ||
  2099.             (theWindowPropToken.tokenProperty == pHasTitleBar) ||
  2100.             (theWindowPropToken.tokenProperty == pHasCloseBox) ||
  2101.                 (theWindowPropToken.tokenProperty == pIsZoomable))
  2102.             {
  2103.                 theBoolean = true;
  2104.                                 
  2105.                 theErr  = AECreateDesc(typeBoolean,
  2106.                                                              (Ptr)&theBoolean,
  2107.                                                              sizeof(theBoolean),
  2108.                                                              dataDesc);
  2109.             }
  2110.         
  2111.         if (theWindowPropToken.tokenProperty == pIsFloating) 
  2112.             {
  2113.                 theBoolean = false;
  2114.                                 
  2115.                 theErr  = AECreateDesc(typeBoolean,
  2116.                                                              (Ptr)&theBoolean,
  2117.                                                              sizeof(theBoolean),
  2118.                                                              dataDesc);
  2119.             }
  2120.             
  2121.         if (theWindowPropToken.tokenProperty == pIsModified)
  2122.             {
  2123.                 theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2124.                 
  2125.                 theBoolean  = theDocument->dirty;
  2126.                 
  2127.                 theErr  = AECreateDesc(typeBoolean,
  2128.                                                              (Ptr)&theBoolean,
  2129.                                                              sizeof(theBoolean),
  2130.                                                              dataDesc);
  2131.             }
  2132.             
  2133.         if (theWindowPropToken.tokenProperty == pIndex) 
  2134.             {
  2135.                 theIndex = 0;
  2136.                 if (theWindowPropToken.tokenWindowToken) 
  2137.                     do
  2138.                         theIndex++;
  2139.                     while (theWindowPropToken.tokenWindowToken != GetWindowPtrOfNthWindow(theIndex));
  2140.                                 
  2141.                 theErr  = AECreateDesc(typeShortInteger,
  2142.                                                              (Ptr)theIndex,
  2143.                                                              sizeof(theIndex),
  2144.                                                              dataDesc);
  2145.             }
  2146.             
  2147.         if (theWindowPropToken.tokenProperty == pPageSetup) 
  2148.             {
  2149.                 theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2150.                 
  2151.                 HLock((Handle)theDocument->thePrintSetup);
  2152.                 
  2153.                 theErr  = AECreateDesc(typeTPrint,
  2154.                                                              (Ptr)*(theDocument->thePrintSetup),
  2155.                                                              sizeof(TPrint),
  2156.                                                              dataDesc);
  2157.                                                              
  2158.                 HUnlock((Handle)theDocument->thePrintSetup);
  2159.                                                              
  2160.             }
  2161.             
  2162.         if (theWindowPropToken.tokenProperty == pSelection) 
  2163.             {
  2164.                 theDocument = DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2165.                 
  2166.                 theErr  = MakeSelTextObj( theWindowPropToken.tokenWindowToken,
  2167.                                                                     theDocument->theText, 
  2168.                                                                     dataDesc);                                                             
  2169.             }
  2170.             
  2171.         return(theErr);
  2172.     } /* GetWindowProperty */
  2173.     
  2174. /** -----------------------------------------------------------------------
  2175.         Name:             GetApplicationProperty
  2176.         Purpose:        Fills dataDesc with the requested application property.
  2177.      -----------------------------------------------------------------------**/
  2178.      
  2179. pascal OSErr GetApplicationProperty(const AEDesc *theObjToken, AEDesc *dataDesc)
  2180.     {
  2181.       OSErr         theErr;
  2182.         Str255        theName;
  2183.         Boolean       isFront;
  2184.         applPropToken theApplPropToken;
  2185.         AEDesc        newDesc;
  2186.         Size          tokenSize;
  2187.         
  2188.         theErr = AECoerceDesc(theObjToken, typeMyApplProp, &newDesc);
  2189.         if (theErr==noErr)
  2190.             {
  2191.                 GetRawDataFromDescriptor(&newDesc,
  2192.                                                                  (Ptr)&theApplPropToken,
  2193.                                                                  sizeof(theApplPropToken),
  2194.                                                                  &tokenSize);
  2195.                                                                  
  2196.                 theErr = AEDisposeDesc(&newDesc);
  2197.             }
  2198.         else
  2199.           return(theErr);
  2200.  
  2201.         theErr = kAEGenericErr;
  2202.         
  2203.         if (theApplPropToken.tokenApplProperty == pName) 
  2204.             {
  2205.                 PLstrcpy((StringPtr)theName, (unsigned char *)"\p7Edit");
  2206.                 theErr  = AECreateDesc(typeChar,
  2207.                                                              (Ptr)&theName[1],
  2208.                                                              theName[0],
  2209.                                                              dataDesc);
  2210.             }
  2211.         
  2212.         if (theApplPropToken.tokenApplProperty == pVersion) 
  2213.             {
  2214.                 PLstrcpy((StringPtr)theName, (unsigned char *)"\p3.0d5");
  2215.                 theErr  = AECreateDesc(typeChar,
  2216.                                                              (Ptr)&theName[1],
  2217.                                                              theName[0],
  2218.                                                              dataDesc);
  2219.             }
  2220.         
  2221.         if (theApplPropToken.tokenApplProperty == pIsFrontProcess) 
  2222.             {
  2223.                 isFront = !gInBackground;
  2224.                 theErr  = AECreateDesc(typeBoolean,
  2225.                                                              (Ptr)&isFront,
  2226.                                                              sizeof(isFront),
  2227.                                                              dataDesc);
  2228.             }
  2229.             
  2230.         return(theErr);
  2231.     } /* GetApplicationProperty */
  2232.  
  2233. /** -----------------------------------------------------------------------
  2234.         Name:             GetMenuProperty
  2235.         Purpose:        Fills dataDesc with the requested menu property.
  2236.      -----------------------------------------------------------------------**/
  2237.      
  2238. pascal OSErr GetMenuProperty(const AEDesc *theObjToken, AEDesc *dataDesc)
  2239.     {
  2240.       OSErr         theErr;
  2241.         Str255        theName;
  2242.         MenuPropToken theMenuPropToken;
  2243.         AEDesc        newDesc;
  2244.         Size          tokenSize;
  2245.         
  2246.         theErr = AECoerceDesc(theObjToken, typeMyMenuProp, &newDesc);
  2247.         if (theErr==noErr)
  2248.             {
  2249.                 GetRawDataFromDescriptor(&newDesc,
  2250.                                                                  (Ptr)&theMenuPropToken,
  2251.                                                                  sizeof(theMenuPropToken),
  2252.                                                                  &tokenSize);
  2253.                                                                  
  2254.                 theErr = AEDisposeDesc(&newDesc);
  2255.             }
  2256.         else
  2257.           return(theErr);
  2258.  
  2259.         theErr = kAEGenericErr;
  2260.         
  2261.         if (theMenuPropToken.theMenuProp == pName) 
  2262.             {
  2263.               PLstrcpy(theName, (**theMenuPropToken.theMenuToken.theTokenMenu).menuData);
  2264.                 theErr  = AECreateDesc(typeChar,
  2265.                                                              (Ptr)&theName[1],
  2266.                                                              theName[0],
  2267.                                                              dataDesc);
  2268.             }
  2269.         
  2270.         if (theMenuPropToken.theMenuProp == pMenuID) 
  2271.             {
  2272.                 theErr  = AECreateDesc(typeShortInteger,
  2273.                                                              (Ptr)&theMenuPropToken.theMenuToken.theTokenID,
  2274.                                                              sizeof(theMenuPropToken.theMenuToken.theTokenID),
  2275.                                                              dataDesc);
  2276.             }
  2277.                                 
  2278.         return(theErr);
  2279.     } /* GetMenuProperty */
  2280.     
  2281. /** -----------------------------------------------------------------------
  2282.         Name:             GetMenuItemProperty
  2283.         Purpose:        Fills dataDesc with the requested menu property.
  2284.      -----------------------------------------------------------------------**/
  2285.      
  2286. pascal OSErr GetMenuItemProperty(const AEDesc *theObjToken, AEDesc *dataDesc)
  2287.     {
  2288.       OSErr             theErr;
  2289.         Str255            theName;
  2290.         MenuItemPropToken theMenuItemPropToken;
  2291.         AEDesc            newDesc;
  2292.         Size              tokenSize;
  2293.         
  2294.         theErr = AECoerceDesc(theObjToken, typeMyItemProp, &newDesc);
  2295.         if (theErr==noErr)
  2296.             {
  2297.                 GetRawDataFromDescriptor(&newDesc,
  2298.                                                                  (Ptr)&theMenuItemPropToken,
  2299.                                                                  sizeof(theMenuItemPropToken),
  2300.                                                                  &tokenSize);
  2301.                                                                  
  2302.                 theErr = AEDisposeDesc(&newDesc);
  2303.             }
  2304.         else
  2305.           return(theErr);
  2306.  
  2307.         theErr = kAEGenericErr;
  2308.         
  2309.         if (theMenuItemPropToken.theItemProp == pName) 
  2310.             {
  2311.               GetItem(theMenuItemPropToken.theItemToken.theMenuToken.theTokenMenu,
  2312.                         theMenuItemPropToken.theItemToken.theTokenItem,
  2313.                                 theName);
  2314.                 theErr  = AECreateDesc(typeChar,
  2315.                                                              (Ptr)&theName[1],
  2316.                                                              theName[0],
  2317.                                                              dataDesc);
  2318.             }
  2319.         
  2320.         if (theMenuItemPropToken.theItemProp == pItemNumber) 
  2321.             {
  2322.                 theErr  = AECreateDesc(typeShortInteger,
  2323.                                                              (Ptr)&theMenuItemPropToken.theItemToken.theTokenItem,
  2324.                                                              sizeof(theMenuItemPropToken.theItemToken.theTokenItem),
  2325.                                                              dataDesc);
  2326.             }
  2327.                     
  2328.         return(theErr);
  2329.     } /* GetMenuItemProperty */
  2330.  
  2331. /** -----------------------------------------------------------------------
  2332.         Name:             HandleGetData
  2333.         Purpose:        Coerces theObj into a token which we understand and
  2334.                                 extracts the data requested in the token and puts it
  2335.                                 into dataDesc.
  2336.      -----------------------------------------------------------------------**/
  2337.      
  2338. typedef char chars[32001];
  2339. typedef chars **charsHandle;
  2340.  
  2341. pascal OSErr HandleGetData(AEDesc *theObj, DescType whatType, AEDesc *dataDesc)
  2342.   {
  2343. #pragma unused (whatType)
  2344.  
  2345.       OSErr           myErr;
  2346.         AEDesc          newDesc;
  2347.         TextToken       theTextToken;
  2348.         Size            tokenSize;
  2349.         DPtr            theDoc;
  2350.         AEDesc          objTokenDesc;
  2351.  
  2352.         myErr = errAEWrongDataType;
  2353.         
  2354.         /*
  2355.             Coerce theObj into a token which we can use - 
  2356.                  set the property for that token
  2357.         */
  2358.     
  2359.     
  2360.     myErr = AEResolve(theObj ,kAEIDoMinimum, &objTokenDesc);
  2361.   
  2362.     if (myErr == noErr)    
  2363.     if (objTokenDesc.descriptorType == typeMyApplProp)
  2364.         myErr = GetApplicationProperty(&objTokenDesc, dataDesc);
  2365.     else
  2366.     
  2367.     if (myErr == noErr)    
  2368.     if (objTokenDesc.descriptorType == typeMyMenuProp)
  2369.         myErr = GetMenuProperty(&objTokenDesc, dataDesc);
  2370.     else
  2371.     
  2372.     if (objTokenDesc.descriptorType == typeMyItemProp)
  2373.         myErr = GetMenuItemProperty(&objTokenDesc, dataDesc);
  2374.     else
  2375.     
  2376.     if (objTokenDesc.descriptorType == typeMyTextProp)
  2377.         myErr = GetTextProperty(&objTokenDesc, dataDesc);
  2378.     else
  2379.     
  2380.     if (objTokenDesc.descriptorType == typeMyWindowProp)
  2381.         myErr = GetWindowProperty(&objTokenDesc, dataDesc);
  2382.     else    
  2383.         if (objTokenDesc.descriptorType == typeMyText)
  2384.             if (AECoerceDesc(&objTokenDesc, typeMyText, &newDesc) == noErr) 
  2385.                 {
  2386.                     GetRawDataFromDescriptor(&newDesc,
  2387.                                                                      (Ptr)&theTextToken,
  2388.                                                                      sizeof(theTextToken),
  2389.                                                                      &tokenSize);
  2390.                     
  2391.                     myErr = AEDisposeDesc(&newDesc);
  2392.                                                                      
  2393.                     theDoc                 = DPtrFromWindowPtr(theTextToken.tokenWindow);
  2394.                     
  2395.                     myErr = BuildStyledTextDesc(theDoc->theText,
  2396.                                                                             theTextToken.tokenOffset,
  2397.                                                                             theTextToken.tokenLength,
  2398.                                                                             dataDesc);
  2399.  
  2400.                 }
  2401.             
  2402.         return(myErr);
  2403.     }    /* HandleGetData */
  2404.  
  2405. /** -----------------------------------------------------------------------
  2406.         Name:             DoGetData
  2407.         Purpose:        Handles the GetData AppleEvent.
  2408.      -----------------------------------------------------------------------**/
  2409.  
  2410. pascal OSErr DoGetData(const AppleEvent *theAppleEvent,
  2411.                                          AppleEvent *reply,
  2412.                                          long handlerRefCon)
  2413.     { 
  2414. #pragma unused (handlerRefCon)
  2415.  
  2416.         OSErr    myErr;
  2417.         OSErr    tempErr;
  2418.         AEDesc   myDirObj;
  2419.         AEDesc   myDataDesc;
  2420.         Size     actualSize;
  2421.         DescType returnedType;
  2422.         DescType reqType;
  2423.         
  2424.         myDataDesc.dataHandle = nil;
  2425.         myDirObj.dataHandle   = nil;
  2426.         
  2427.         /*
  2428.             extract the direct object, which is the object whose data is to be returned
  2429.         */
  2430.         
  2431.         myErr  = AEGetParamDesc(theAppleEvent,
  2432.                                                         keyDirectObject,
  2433.                                                         typeWildCard,
  2434.                                                         &myDirObj);
  2435.             
  2436.         /*
  2437.             now the get the type of data wanted - optional
  2438.         */
  2439.         
  2440.         tempErr = AEGetParamPtr(theAppleEvent,
  2441.                                                         keyAERequestedType,
  2442.                                                         typeType,
  2443.                                                         &returnedType,
  2444.                                                         (Ptr)&reqType,
  2445.                                                         sizeof(reqType),
  2446.                                                         &actualSize);
  2447.         
  2448.         if (tempErr!=noErr) 
  2449.             reqType = typeChar;
  2450.             
  2451.         if (myErr == noErr) 
  2452.             myErr = GotRequiredParams(theAppleEvent);
  2453.         
  2454.         /* get the data */
  2455.         if (myErr == noErr) 
  2456.             myErr = HandleGetData(&myDirObj, reqType, &myDataDesc);
  2457.             
  2458.         /* if they wanted a reply, attach it now */
  2459.         if (myErr==noErr) 
  2460.             if (reply->descriptorType != typeNull) 
  2461.                 myErr = AEPutParamDesc(reply, keyDirectObject, &myDataDesc);
  2462.                 
  2463.     if (myDataDesc.dataHandle)
  2464.           tempErr = AEDisposeDesc(&myDataDesc);
  2465.  
  2466.     if (myDirObj.dataHandle)
  2467.           tempErr = AEDisposeDesc(&myDirObj);
  2468.             
  2469.         return(myErr);
  2470.     }    /* DoGetData */
  2471.  
  2472.  
  2473. /** -----------------------------------------------------------------------
  2474.         Name:             DoGetDataSize
  2475.         Purpose:        Handles the GetDataSize AppleEvent.
  2476.      -----------------------------------------------------------------------**/
  2477.  
  2478. pascal OSErr DoGetDataSize(const AppleEvent *theAppleEvent,
  2479.                                                         AppleEvent *reply,
  2480.                                                                  long       handlerRefCon)
  2481.   { 
  2482. #pragma unused (handlerRefCon)
  2483.     
  2484.         OSErr     myErr;
  2485.         OSErr     tempErr;
  2486.         AEDesc    myDirObj;
  2487.         AEDesc    myDataDesc;
  2488.         Size      actualSize;
  2489.         DescType  returnedType;
  2490.         DescType  reqType;
  2491.         long      dataSize;
  2492.         
  2493.         myDataDesc.dataHandle = nil;
  2494.         myDirObj.dataHandle = nil;
  2495.         
  2496.         /* pick up the direct object, which is the object whose data is to be sized */
  2497.         
  2498.         myErr  = AEGetParamDesc(theAppleEvent,
  2499.                                                         keyDirectObject,
  2500.                                                         typeWildCard,
  2501.                                                         &myDirObj);
  2502.             
  2503.         /* now the get the type wanted - optional*/
  2504.         
  2505.         tempErr = AEGetParamPtr(theAppleEvent,
  2506.                                                         keyAERequestedType,
  2507.                                                         typeType,
  2508.                                                         &returnedType,
  2509.                                                         (Ptr)&reqType,
  2510.                                                         sizeof(reqType),
  2511.                                                         &actualSize);
  2512.         
  2513.         if (tempErr!=noErr) 
  2514.             reqType = typeChar;
  2515.             
  2516.         if (myErr == noErr) 
  2517.             myErr = GotRequiredParams(theAppleEvent);
  2518.         
  2519.         /* get the data */
  2520.         if (myErr == noErr) 
  2521.             myErr = HandleGetData(&myDirObj, reqType, &myDataDesc);
  2522.             
  2523.         /* evaluate size of data and discard, create desc for size */
  2524.         if (myErr == noErr) 
  2525.             if (myDataDesc.dataHandle) 
  2526.                 {
  2527.                     dataSize = GetHandleSize((Handle)myDataDesc.dataHandle);
  2528.                     DisposHandle((Handle)myDataDesc.dataHandle);
  2529.                     myErr  = AECreateDesc(typeLongInteger,
  2530.                                                                 (Ptr)&dataSize,
  2531.                                                                 sizeof(dataSize),
  2532.                                                                 &myDataDesc);
  2533.                 }
  2534.             
  2535.             
  2536.         /* if they wanted a reply, attach it now */
  2537.         
  2538.         if (myErr==noErr) 
  2539.             if (reply->descriptorType != typeNull) 
  2540.                 myErr = AEPutParamDesc(reply, keyDirectObject, &myDataDesc);
  2541.                 
  2542.         /* discard our copy */
  2543.         
  2544.         if (myDataDesc.dataHandle) 
  2545.             tempErr = AEDisposeDesc(&myDataDesc);
  2546.                 
  2547.         if (myDirObj.dataHandle) 
  2548.             tempErr = AEDisposeDesc(&myDirObj);
  2549.                 
  2550.         return(myErr);
  2551.     }    /* DoGetDataSize */
  2552.  
  2553. /** -----------------------------------------------------------------------
  2554.         Name:             DoNewElement
  2555.         Purpose:        Handles the NewElement AppleEvent. Only Creates windows for
  2556.                     now.
  2557.      -----------------------------------------------------------------------**/
  2558.  
  2559. pascal OSErr DoNewElement(const AppleEvent *theAppleEvent,
  2560.                                                 AppleEvent *reply, 
  2561.                                                 long       handlerRefCon)
  2562. {
  2563. #pragma unused (handlerRefCon)
  2564.  
  2565.     OSErr       myErr;
  2566.     OSErr       ignoreErr;
  2567.     DescType      returnedType;
  2568.     DescType      newElemClass;
  2569.     Size        actSize;
  2570.     AEDesc        wndwObjSpec;
  2571.     DPtr        theDoc;
  2572.     
  2573.     wndwObjSpec.dataHandle = nil;
  2574.         
  2575.     myErr = AEGetParamPtr(theAppleEvent,
  2576.                                                 keyAEObjectClass,
  2577.                                                 typeType,
  2578.                                                 &returnedType,
  2579.                                                 (Ptr)&newElemClass,
  2580.                                                 sizeof(newElemClass),
  2581.                                                 &actSize);
  2582.             
  2583.   /* check for missing required parameters */
  2584.     
  2585.   if (myErr == noErr) 
  2586.         myErr = GotRequiredParams(theAppleEvent);
  2587.   
  2588.   /* got all required params */
  2589.   
  2590.   /* let's make sure container is the null desc */
  2591.   /* and they want a window */
  2592.     
  2593.   if (newElemClass != cWindow) 
  2594.     myErr = errAEWrongDataType;
  2595.   
  2596.   /* let's create a new window */
  2597.     
  2598.     if (myErr == noErr) 
  2599.         theDoc = NewDocument(false);
  2600.   
  2601.     if (myErr==noErr)
  2602.         if (theDoc == nil) 
  2603.             myErr = -1700;
  2604.         else
  2605.             {
  2606.                 ShowWindow(theDoc->theWindow);
  2607.                 theDoc->dirty = false;
  2608.             
  2609.                 myErr = MakeWindowObj(theDoc->theWindow, &wndwObjSpec);
  2610.             }
  2611.       
  2612.     if (myErr == noErr) 
  2613.         if (reply->descriptorType != typeNull) 
  2614.              myErr = AEPutParamDesc(reply,
  2615.                                                             keyDirectObject,
  2616.                                                             &wndwObjSpec);
  2617.             
  2618.     if (wndwObjSpec.dataHandle) 
  2619.         ignoreErr = AEDisposeDesc(&wndwObjSpec);
  2620.         
  2621.   return(myErr);
  2622. }    /* DoNewElement */
  2623.  
  2624. /** -----------------------------------------------------------------------
  2625.         Name:             DoIsThereA
  2626.         Purpose:        Handles the IsThereA AppleEvent.
  2627.      -----------------------------------------------------------------------**/
  2628.  
  2629. pascal OSErr DoIsThereA(const AppleEvent *theAppleEvent,
  2630.                                             AppleEvent       *reply, 
  2631.                                             long             handlerRefCon)
  2632.                                             
  2633. /*
  2634.     Support check of Windows at first
  2635.     
  2636.     What we do :
  2637.         Get Direct Object
  2638.         Check have all required params
  2639.         Coerce into things we support
  2640.         if we get something back
  2641.             check to see it exists and set reply
  2642.         clean up
  2643.         return
  2644. */
  2645.  
  2646.  {
  2647. #pragma unused (handlerRefCon)
  2648.  
  2649.         OSErr         myErr;
  2650.         OSErr         ignoreErr;
  2651.         AEDesc        myDirObject;
  2652.         AEDesc        windDesc;
  2653.         AEDesc        dataDesc;
  2654.         WindowToken   theWindowToken;
  2655.         Size          tokenSize;
  2656.         Boolean       exists;
  2657.  
  2658.         myDirObject.dataHandle = nil;
  2659.         windDesc.dataHandle    = nil;
  2660.         dataDesc.dataHandle    = nil;
  2661.                 
  2662.         myErr = AEGetParamDesc(theAppleEvent,
  2663.                                                      keyDirectObject,
  2664.                                                      typeWildCard,
  2665.                                                      &myDirObject);
  2666.                         
  2667.         /* check for missing required parameters */
  2668.         
  2669.         if (myErr == noErr) 
  2670.             myErr = GotRequiredParams(theAppleEvent);
  2671.         
  2672.         /* got all required params */
  2673.         
  2674.         /* let's make sure they want to check for a window */
  2675.             
  2676.         exists = false;
  2677.         
  2678.         if (myErr == noErr) 
  2679.             if (AECoerceDesc(&myDirObject, typeMyWndw, &windDesc)==noErr) 
  2680.                 if (windDesc.descriptorType!=typeNull) 
  2681.                     {
  2682.                         GetRawDataFromDescriptor(&windDesc,
  2683.                                                                          (Ptr)&theWindowToken,
  2684.                                                                          sizeof(theWindowToken),
  2685.                                                                          &tokenSize);
  2686.                                                                          
  2687.                         exists = (theWindowToken != nil);    
  2688.                     }
  2689.                     
  2690.         if (myErr == noErr) 
  2691.             myErr = AECreateDesc(typeBoolean,
  2692.                                                      (Ptr)&exists,
  2693.                                                      sizeof(exists),
  2694.                                                      &dataDesc);
  2695.     
  2696.             
  2697.         /* 
  2698.             if they wanted a reply, which they surely must, 
  2699.             attach the result to it…
  2700.         */
  2701.             
  2702.         if (myErr == noErr)
  2703.             if (reply->descriptorType != typeNull) 
  2704.                  myErr = AEPutParamDesc(reply,
  2705.                                                                 keyDirectObject,
  2706.                                                                 &dataDesc);
  2707.                 
  2708.         if (dataDesc.dataHandle) 
  2709.             ignoreErr = AEDisposeDesc(&dataDesc);
  2710.             
  2711.         if (myDirObject.dataHandle) 
  2712.             ignoreErr = AEDisposeDesc(&myDirObject);
  2713.             
  2714.         if (windDesc.dataHandle) 
  2715.             ignoreErr = AEDisposeDesc(&windDesc);
  2716.             
  2717.         return(myErr);
  2718.     }    /* DoIsThereA */
  2719.  
  2720. /** -----------------------------------------------------------------------
  2721.         Name:             DoCloseWindow
  2722.         Purpose:        Handles the Close AppleEvent.
  2723.      -----------------------------------------------------------------------**/
  2724.  
  2725. pascal OSErr DoCloseWindow(const AppleEvent *theAppleEvent,
  2726.                                                     AppleEvent       *reply,
  2727.                                                     long             handlerRefCon)
  2728.     { 
  2729. #pragma unused (reply, handlerRefCon)
  2730.  
  2731.         OSErr         myErr;
  2732.         OSErr         tempErr;
  2733.         AEDesc        myDirObj;
  2734.         AEDesc        newDesc;
  2735.         WindowToken   theWindowToken;
  2736.         Size          tokenSize;
  2737.         DescType      saveOpt;
  2738.         Size          actSize;
  2739.         DescType      returnedType;
  2740.         DPtr          myDPtr;
  2741.  
  2742.         myDirObj.dataHandle = nil;
  2743.         
  2744.         /* pick up the direct object, which is the object (window) to close */
  2745.         
  2746.         myErr = AEGetParamDesc(theAppleEvent,
  2747.                                                      keyDirectObject,
  2748.                                                      typeWildCard,
  2749.                                                      &myDirObj);
  2750.                     
  2751.         /* pick up optional save param, if any */
  2752.         
  2753.         saveOpt = kAEAsk; /* the default */
  2754.         
  2755.         tempErr = AEGetParamPtr(theAppleEvent,
  2756.                                                         keyAESaveOptions,
  2757.                                                         typeEnumerated,
  2758.                                                         &returnedType,
  2759.                                                         (Ptr)&saveOpt,
  2760.                                                         sizeof(saveOpt),
  2761.                                                         &actSize);
  2762.                                                          
  2763.         if (myErr == noErr)
  2764.             myErr = GotRequiredParams(theAppleEvent);
  2765.             
  2766.         /* get the window to close as a window ptr */
  2767.         if (myErr == noErr) 
  2768.             if (AECoerceDesc(&myDirObj, typeMyWndw, &newDesc)==noErr) 
  2769.                 if (newDesc.descriptorType!=typeNull) 
  2770.                     {
  2771.                         GetRawDataFromDescriptor(&newDesc,
  2772.                                                                          (Ptr)&theWindowToken,
  2773.                                                                          sizeof(theWindowToken),
  2774.                                                                          &tokenSize);
  2775.                                                                          
  2776.                         myErr = AEDisposeDesc(&newDesc);
  2777.                         
  2778.                         if (theWindowToken) 
  2779.                             {
  2780.                                 myErr=AESetInteractionAllowed(kAEInteractWithAll); /* Should do this in prefs */
  2781.                                 
  2782.                                 /*
  2783.                                     We do some of the close checks here to avoid
  2784.                                     calling AEInteractWithUser
  2785.                                 */
  2786.                                 myDPtr = DPtrFromWindowPtr(theWindowToken);
  2787.                                 if ((myDPtr->dirty) || (myDPtr->everSaved == false)) 
  2788.                                     if (saveOpt != kAENo) /* Don't flip layers if force no ask */
  2789.                                         myErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  2790.                                     
  2791.                                 if (myErr==noErr) 
  2792.                                     myErr = DoClose(theWindowToken, true, saveOpt);
  2793.                             }
  2794.                         else
  2795.                             myErr =  errAEIllegalIndex;
  2796.                     }
  2797.                         
  2798.         if (myDirObj.dataHandle) 
  2799.             tempErr = AEDisposeDesc(&myDirObj);
  2800.                 
  2801.         return(myErr);
  2802.     }    /* DoCloseWindow */
  2803.  
  2804. /** -----------------------------------------------------------------------
  2805.         Name:             DoSaveWindow
  2806.         Purpose:        Handles the Save AppleEvent.
  2807.      -----------------------------------------------------------------------**/
  2808.  
  2809. pascal OSErr DoSaveWindow(const AppleEvent *theAppleEvent,
  2810.                                                  AppleEvent       *reply,
  2811.                                                  long             handlerRefCon)
  2812.     { 
  2813. #pragma unused (reply, handlerRefCon)
  2814.     
  2815.         OSErr         myErr;
  2816.         OSErr         tempErr;
  2817.         AEDesc        myDirObj;
  2818.         AEDesc        newDesc;
  2819.         WindowToken   theWindowToken;
  2820.         Size          tokenSize;
  2821.         Size          actSize;
  2822.         DescType      returnedType;
  2823.         DPtr          theDoc;
  2824.         FSSpec        destFSSpec;
  2825.         
  2826.         myDirObj.dataHandle = nil;
  2827.         
  2828.         /* pick up the direct object, which is the window to save */
  2829.         
  2830.         myErr = AEGetParamDesc(theAppleEvent,
  2831.                                                      keyDirectObject,
  2832.                                                      typeWildCard,
  2833.                                                      &myDirObj);
  2834.                                                      
  2835.         /* pick up optional destination param, if any */
  2836.     
  2837.         tempErr = AEGetParamPtr(theAppleEvent,
  2838.                                                         keyAEDestination,
  2839.                                                       typeFSS,
  2840.                                                         &returnedType,
  2841.                                                         (Ptr)&destFSSpec,
  2842.                                                         sizeof(destFSSpec),
  2843.                                                         &actSize);
  2844.     
  2845.         if (myErr == noErr) 
  2846.             myErr = GotRequiredParams(theAppleEvent);
  2847.             
  2848.         /* get the data */
  2849.     
  2850.         myErr = AECoerceDesc(&myDirObj, typeMyWndw, &newDesc);
  2851.         
  2852.         if (myErr == noErr)
  2853.             if (newDesc.descriptorType!=typeNull) 
  2854.                 {
  2855.                     GetRawDataFromDescriptor(&newDesc,
  2856.                                                                      (Ptr)&theWindowToken,
  2857.                                                                      sizeof(theWindowToken),
  2858.                                                                      &tokenSize);
  2859.                                                                      
  2860.                     myErr = AEDisposeDesc(&newDesc);
  2861.                                                                      
  2862.                     if (theWindowToken) 
  2863.                         {                            
  2864.                             theDoc = DPtrFromWindowPtr(theWindowToken);
  2865.                             
  2866.                             if (theDoc->everSaved == false) 
  2867.                                 if (tempErr != noErr) 
  2868.                                      /* We had no supplied destination and no default either */
  2869.                                     myErr = kAEGenericErr;
  2870.                             
  2871.                             if (myErr==noErr)
  2872.                                 if (tempErr==noErr)
  2873.                                     { /* we were told where */
  2874.                                         myErr = DoSave(theDoc, destFSSpec); 
  2875.                                         
  2876.                                         if (myErr==noErr)
  2877.                                             AssocAllSections(theDoc);
  2878.                                     }
  2879.                                 else
  2880.                                     myErr = SaveUsingTemp(theDoc);
  2881.                         }
  2882.                     else
  2883.                         myErr =  errAEIllegalIndex;
  2884.                 }
  2885.                 
  2886.         if (myDirObj.dataHandle) 
  2887.             tempErr = AEDisposeDesc(&myDirObj);
  2888.     
  2889.         return(myErr);
  2890.                     
  2891.     }    /* DoSaveWindow */
  2892.  
  2893. /** -----------------------------------------------------------------------
  2894.         Name:             DoRevertWindow
  2895.         Purpose:        Handles the Revert AppleEvent.
  2896.      -----------------------------------------------------------------------**/
  2897.  
  2898. pascal OSErr DoRevertWindow(const AppleEvent *theAppleEvent,
  2899.                                                      AppleEvent       *reply,
  2900.                                                      long             handlerRefCon)
  2901. #pragma unused (reply, handlerRefCon)
  2902.  
  2903.     OSErr          myErr;
  2904.     OSErr          ignoreErr;
  2905.     AEDesc         myDirObj;
  2906.     AEDesc         newDesc;
  2907.     WindowToken    theWindowToken;
  2908.     Size           tokenSize;
  2909.     DPtr           theDoc;
  2910.     
  2911.     myDirObj.dataHandle = nil;
  2912.     
  2913.   /* pick up the direct object, which is the window to save */
  2914.     
  2915.   myErr = AEGetParamDesc(theAppleEvent,
  2916.                                                  keyDirectObject,
  2917.                                                  typeWildCard,
  2918.                                                  &myDirObj);
  2919.  
  2920.     if (myErr == noErr)
  2921.       myErr = GotRequiredParams(theAppleEvent);
  2922.         
  2923.   /* get the window to revert from the direct object */
  2924.     
  2925.     myErr = AECoerceDesc(&myDirObj, typeMyWndw, &newDesc);
  2926.     
  2927.   if (myErr == noErr) 
  2928.         if (newDesc.descriptorType!=typeNull) 
  2929.             {
  2930.                 GetRawDataFromDescriptor(&newDesc,
  2931.                                                                  (Ptr)&theWindowToken,
  2932.                                                                  sizeof(theWindowToken),
  2933.                                                                  &tokenSize);
  2934.                                                                  
  2935.                 myErr = AEDisposeDesc(&newDesc);
  2936.                                                                  
  2937.                 if (theWindowToken) 
  2938.                     {    
  2939.                         theDoc = DPtrFromWindowPtr(theWindowToken);
  2940.                         
  2941.                         HidePen();
  2942.                         TESetSelect(0, (*(theDoc->theText))->teLength, theDoc->theText);
  2943.                         ShowPen();
  2944.                         TEDelete(theDoc->theText);
  2945.         
  2946.                         if (theDoc->everSaved) 
  2947.                             {
  2948.                                 myErr = GetFileContents(theDoc->theFSSpec, theDoc);
  2949.                                 if (myErr == noErr) 
  2950.                                     {
  2951.                                         ResizeWindow(theDoc);
  2952.                                         theDoc->dirty = false;
  2953.                                     }
  2954.                             }
  2955.         
  2956.                         ShowWindow(theDoc->theWindow); /* <<< Visible already??? */
  2957.                         DoUpdate(theDoc);
  2958.                     }
  2959.                 else
  2960.                     myErr =  errAEIllegalIndex;
  2961.             }
  2962.                 
  2963.     if (myDirObj.dataHandle) 
  2964.         ignoreErr = AEDisposeDesc(&myDirObj);
  2965.         
  2966.   return(myErr);
  2967. }    /* DoRevertWindow */
  2968.  
  2969. /**-----------------------------------------------------------------------
  2970.         Name:             DoPrintDocuments
  2971.         Purpose:        Print a list of documents (or windows).
  2972. -----------------------------------------------------------------------**/
  2973. pascal OSErr DoPrintDocuments(const AppleEvent *message,
  2974.                               AppleEvent       *reply,
  2975.                                                             long refcon)
  2976. {
  2977. #pragma unused (reply, refcon)
  2978.         long          index;
  2979.         long          itemsInList;
  2980.         AEKeyword     keywd;
  2981.         OSErr         err;
  2982.         AEDescList    docList;
  2983.         Size          actSize;
  2984.         DescType      typeCode;
  2985.         FSSpec        theFSSpec;
  2986.         WindowToken   theWindowToken;
  2987.         OSErr         forgetErr;
  2988.         Boolean       talkToUser;
  2989.             
  2990.         err = AEGetParamDesc(message,
  2991.                                                  keyDirectObject,
  2992.                                                  typeAEList,
  2993.                                                  &docList);
  2994.                                                  
  2995.         err = AECountItems(&docList, &itemsInList);
  2996.             
  2997.         for (index = 1; index<=itemsInList; index++)
  2998.             if (err == noErr) 
  2999.                 {
  3000.                     forgetErr = AEGetNthPtr( &docList, index, typeFSS, &keywd,
  3001.                                                                      &typeCode, (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize);
  3002.                                                                         
  3003.                     if (forgetErr == noErr) 
  3004.                         {
  3005.                             if (err == noErr) 
  3006.                                 err = IssueAEOpenDoc(theFSSpec);
  3007.                                 
  3008.                             if (err == noErr) 
  3009.                                 IssuePrintWindow(FrontWindow());
  3010.                                 
  3011.                             if (err == noErr) 
  3012.                                 IssueCloseCommand(FrontWindow());
  3013.                         }
  3014.                     else
  3015.                         { /* wasn't a file - was it a window ? */
  3016.                             err = AEGetNthPtr(&docList,
  3017.                                               index,
  3018.                                               typeMyWndw,
  3019.                                               &keywd,
  3020.                                                                 &typeCode,
  3021.                                                                 (Ptr)&theWindowToken,
  3022.                                                                 sizeof(WindowToken),
  3023.                                                                 &actSize);
  3024.                                                                     
  3025.                             talkToUser = (AEInteractWithUser(kAEDefaultTimeout, nil, nil) == noErr);
  3026.                             
  3027.                             if (err == noErr) 
  3028.                                 PrintWindow(DPtrFromWindowPtr(theWindowToken), talkToUser);
  3029.                         }
  3030.                 }
  3031.         
  3032.         if (docList.dataHandle)
  3033.             forgetErr = AEDisposeDesc(&docList);
  3034.             
  3035.         return(err);
  3036.     } /* DoPrintDocuments */
  3037.  
  3038. /**-----------------------------------------------------------------------
  3039.         Name:             HandleCreatePub
  3040.         Purpose:        Create a publisher.
  3041. -----------------------------------------------------------------------**/
  3042. pascal OSErr HandleCreatePub(const AppleEvent *theAppleEvent,
  3043.                              AppleEvent       *reply,
  3044.                                                          long refcon)
  3045.     {
  3046. #pragma unused (reply, refcon)
  3047.  
  3048.         OSErr       myErr;
  3049.         FSSpec      theFSSpec;
  3050.         OSErr       forgetErr;
  3051.         OSErr       forget2Err;
  3052.         AEDesc      myDirObj;
  3053.         AEDesc      myFileLoc;
  3054.         TextToken   theTextToken;
  3055.         DPtr        theDoc;
  3056.         AEDesc      newDesc;
  3057.         long        tokenSize;
  3058.         Boolean     haveFSSpec;
  3059.         
  3060.         myErr = noErr;
  3061.         
  3062.         forgetErr  = AEGetParamDesc(theAppleEvent,
  3063.                                                                 keyDirectObject,
  3064.                                                                 typeWildCard,
  3065.                                                                 &myDirObj);
  3066.         
  3067.         forget2Err = AEGetParamDesc(theAppleEvent,
  3068.                                                                 keyAEEditionFileLoc,
  3069.                                                                 typeWildCard,
  3070.                                                                 &myFileLoc);
  3071.                                                                  
  3072.         if (myErr==noErr)
  3073.             myErr = GotRequiredParams(theAppleEvent);
  3074.             
  3075.         if (forgetErr==noErr)  /* Set the selection to the supplied object - if any */
  3076.             {
  3077.                 forgetErr = AECoerceDesc(&myDirObj, typeMyText, &newDesc);
  3078.                 if (newDesc.descriptorType!=typeNull) 
  3079.                     {
  3080.                         GetRawDataFromDescriptor(&newDesc,
  3081.                                                                          (Ptr)&theTextToken,
  3082.                                                                          sizeof(theTextToken),
  3083.                                                                          &tokenSize);
  3084.                                                                          
  3085.                         theDoc = DPtrFromWindowPtr(theTextToken.tokenWindow);
  3086.                         
  3087.                         TESetSelect(theTextToken.tokenOffset-1,
  3088.                                                 theTextToken.tokenOffset+
  3089.                                                                     theTextToken.tokenLength-1,
  3090.                                                 theDoc->theText);
  3091.                                                 
  3092.                         forgetErr = AEDisposeDesc(&newDesc);
  3093.                     }
  3094.             }
  3095.         else
  3096.             theDoc = DPtrFromWindowPtr(FrontWindow());
  3097.             
  3098.         if (theDoc==nil) 
  3099.             {
  3100.                 /* Should clean up and exit with error */
  3101.             }
  3102.             
  3103.         haveFSSpec = false;
  3104.         
  3105.         if (forget2Err==noErr)  /* Get the Edition Container File */
  3106.             {
  3107.                 forget2Err = AECoerceDesc(&myDirObj,typeFSS,&newDesc);
  3108.                 if (newDesc.descriptorType!=typeNull) 
  3109.                     {
  3110.                         GetRawDataFromDescriptor(&newDesc,
  3111.                                                                          (Ptr)&theFSSpec,
  3112.                                                                          sizeof(theFSSpec),
  3113.                                                                          &tokenSize);
  3114.                         forget2Err = AEDisposeDesc(&newDesc);
  3115.                         haveFSSpec = true;
  3116.                     }
  3117.             }
  3118.             
  3119.         if (haveFSSpec==false) 
  3120.             myErr = GetEditionContainer(theDoc, &theFSSpec);
  3121.     
  3122.         if (myErr == noErr) 
  3123.             myErr = PublishText(theDoc, &theFSSpec);
  3124.             
  3125.         if (myDirObj.dataHandle)
  3126.             forgetErr = AEDisposeDesc(&myDirObj);
  3127.         
  3128.         if (myFileLoc.dataHandle) 
  3129.             forgetErr = AEDisposeDesc(&myFileLoc);
  3130.         
  3131.         return(myErr);
  3132.         
  3133.     } /* HandleCreatePub */
  3134.  
  3135.  
  3136. pascal OSErr MyCountProc(    DescType desiredType,
  3137.                                                     DescType containerClass,
  3138.                                                     const AEDesc *container,
  3139.                                                     long *result);
  3140.                                             
  3141. /** -----------------------------------------------------------------------
  3142.         Name:       HandleNumberOfElements
  3143.         Purpose:        Handles the Number Of Elements AppleEvent.
  3144.      -----------------------------------------------------------------------**/
  3145.  
  3146. pascal OSErr HandleNumberOfElements(const AppleEvent *theAppleEvent,
  3147.                                                                     AppleEvent *reply,
  3148.                                                                     long       handlerRefCon)
  3149.   {
  3150. #pragma unused (handlerRefCon)
  3151.  
  3152.       OSErr    myErr;
  3153.       OSErr    forgetErr;
  3154.         AEDesc   myDirObj;
  3155.         DescType myClass;
  3156.         long     myCount;
  3157.         DescType returnedType;
  3158.         Size     actSize;
  3159.         
  3160.         myErr                 = errAEEventNotHandled;
  3161.         myDirObj.dataHandle = nil;
  3162.         
  3163.         /* pick up direct object, which is the container in which things are to be counted */
  3164.         
  3165.         myErr = AEGetParamDesc(theAppleEvent,
  3166.                                                      keyDirectObject,
  3167.                                                      typeWildCard,
  3168.                                                      &myDirObj);
  3169.             
  3170.         /* now the class of objects to be counted */
  3171.         
  3172.         myErr = AEGetParamPtr( theAppleEvent,
  3173.                                                      keyAEObjectClass,
  3174.                                                      typeType,
  3175.                                                      &returnedType,
  3176.                                                      (Ptr)&myClass,
  3177.                                                      sizeof(myClass),
  3178.                                                      &actSize);
  3179.             
  3180.         /* missing any parameters? */
  3181.         
  3182.         myErr = GotRequiredParams(theAppleEvent);
  3183.         
  3184.         /* now count */
  3185.     
  3186.         if (myErr == noErr) 
  3187.             myErr = MyCountProc(myClass,myDirObj.descriptorType, &myDirObj,&myCount);
  3188.         
  3189.         /* add result to reply */
  3190.     
  3191.         if (myErr == noErr) 
  3192.             if (reply->descriptorType != typeNull) 
  3193.                  myErr  = AEPutParamPtr(reply,
  3194.                                                                 keyDirectObject,
  3195.                                                                 typeLongInteger,
  3196.                                                                 (Ptr)&myCount,
  3197.                                                                 sizeof(myCount));
  3198.         if (myErr == noErr)       
  3199.             forgetErr  = AEDisposeDesc(&myDirObj);
  3200.         
  3201.         return(myErr);
  3202.         
  3203.     }    /* HandleNumberOfElements */
  3204.  
  3205. /** -----------------------------------------------------------------------
  3206.         Name:             HandleShowSelection
  3207.         Purpose:        Handles the Make Selection Visible AppleEvent.
  3208.      -----------------------------------------------------------------------**/
  3209.  
  3210. pascal OSErr HandleShowSelection(const AppleEvent *theAppleEvent,
  3211.                                                              AppleEvent       *reply,
  3212.                                                              long             handlerRefCon)
  3213.   {
  3214. #pragma unused (reply,handlerRefCon)
  3215.     
  3216.         OSErr       myErr;
  3217.         OSErr       ignoreErr;
  3218.         AEDesc      myDirObj;
  3219.         AEDesc      newDesc;
  3220.         AEDesc      tokenDesc;
  3221.         Size        actSize;
  3222.         WindowToken theWindowToken;
  3223.         DPtr        theDocument;
  3224.         TEHandle    theHTE;
  3225.         
  3226.         myErr      = errAEEventNotHandled;
  3227.         myDirObj.dataHandle  = nil;
  3228.         tokenDesc.dataHandle = nil;
  3229.         
  3230.         /*
  3231.             pick up direct object, i.e. the window in which to show the selection
  3232.         */
  3233.             
  3234.         myErr  = AEGetParamDesc(theAppleEvent,
  3235.                                                         keyDirectObject,
  3236.                                                         typeWildCard,
  3237.                                                         &myDirObj);
  3238.                 
  3239.         /*
  3240.             missing any parameters?
  3241.         */
  3242.         
  3243.         myErr = GotRequiredParams(theAppleEvent);
  3244.         
  3245.         /*
  3246.             convert object to WindowToken which we understand
  3247.         */
  3248.         myErr = AEResolve(&myDirObj, kAEIDoMinimum, &tokenDesc);
  3249.         
  3250.         if (myErr == noErr)
  3251.             if (tokenDesc.descriptorType==typeMyWndw)
  3252.                 {
  3253.                     if (AECoerceDesc(&myDirObj, typeMyWndw, &newDesc) == noErr) 
  3254.                         {
  3255.                             GetRawDataFromDescriptor(&newDesc,
  3256.                                                                              (Ptr)&theWindowToken,
  3257.                                                                              sizeof(theWindowToken),
  3258.                                                                              &actSize);
  3259.         
  3260.                             ignoreErr = AEDisposeDesc(&newDesc);
  3261.                             
  3262.                             if (myErr==noErr) 
  3263.                                 if (theWindowToken)                                                          
  3264.                                     ShowSelect(DPtrFromWindowPtr(theWindowToken));
  3265.                                 else
  3266.                                     myErr = errAEIllegalIndex;
  3267.                         }
  3268.                 }
  3269.             else
  3270.                 if (tokenDesc.descriptorType==typeMyText)
  3271.                     {
  3272.                         myErr = SetSelectionOfAppleEventObject(keyDirectObject,
  3273.                                                                                           theAppleEvent,
  3274.                                                                                            &theDocument,
  3275.                                                                                           &theHTE);
  3276.                         if (theDocument)
  3277.                           ShowSelect(theDocument);
  3278.                         else
  3279.                             myErr = errAEIllegalIndex;
  3280.                     }
  3281.                 else
  3282.                     myErr = errAEEventNotHandled;
  3283.                     
  3284.         if (myDirObj.dataHandle)
  3285.             ignoreErr = AEDisposeDesc(&myDirObj);
  3286.         
  3287.         if (tokenDesc.dataHandle)
  3288.             ignoreErr = AEDisposeDesc(&tokenDesc);
  3289.             
  3290.         return(myErr);
  3291.         
  3292.     }    /* HandleShowSelection */
  3293.  
  3294. pascal OSErr HandleStartRecording(const AppleEvent *theAppleEvent,
  3295.                                                               AppleEvent *reply,
  3296.                                                               long       handlerRefCon)        
  3297.     {
  3298. #pragma unused (reply,handlerRefCon)
  3299.  
  3300.         OSErr myErr;
  3301.  
  3302.       gBigBrother++;
  3303.     
  3304.         myErr = GotRequiredParams(theAppleEvent);
  3305.  
  3306.         return(myErr);
  3307.         
  3308.     }    /* HandleStartRecording */
  3309.  
  3310. pascal OSErr HandleStopRecording(const AppleEvent *theAppleEvent,
  3311.                                                              AppleEvent *reply,
  3312.                                                                  long handlerRefCon)        
  3313.     {
  3314. #pragma unused (theAppleEvent,reply,handlerRefCon)
  3315.     
  3316.         gBigBrother--;
  3317.         return(noErr);
  3318.     }    /* HandleStopRecording */
  3319.  
  3320.  
  3321. #pragma segment AECommandIssuers
  3322.  
  3323. /*******************************************************************************/
  3324. /* 
  3325.         Start of section involved in building and sending AppleEvent Objects as/with
  3326.         commands
  3327.  */
  3328.  
  3329. /*
  3330.     Make an AEDesc that describes the selection in the window and text edit
  3331.     record supplied
  3332. */
  3333.  
  3334. pascal OSErr MakeWindowObj(WindowPtr theWindow,
  3335.                                                    AEDesc    *dMyDoc)
  3336.     {
  3337.             AEDesc   dNull;
  3338.             AEDesc   dDocName;
  3339.             Str255   windowName;
  3340.             OSErr    myErr;
  3341.             
  3342.             GetWTitle(theWindow, windowName);
  3343.             myErr = AECreateDesc(typeChar,(Ptr)&windowName[1], windowName[0], &dDocName);
  3344.             
  3345.             if (myErr==noErr) 
  3346.                 myErr = AECreateDesc(typeNull, nil , 0, &dNull);
  3347.             
  3348.             if (myErr==noErr) 
  3349.                 myErr = CreateObjSpecifier(cWindow, &dNull, formName, &dDocName, true, dMyDoc);
  3350.             
  3351.             return(myErr);
  3352.             
  3353.     } /*MakeWindowObj*/
  3354.     
  3355. pascal OSErr MakeTextObj(WindowPtr theWindow,
  3356.                                                  short     selStart,
  3357.                                                  short     selEnd,
  3358.                                                AEDesc    *selTextObj)
  3359.     {
  3360.         OSErr    myErr;
  3361.         OSErr    ignoreErr;
  3362.         AEDesc   dMyDoc;
  3363.         AEDesc   startOfs;
  3364.         AEDesc   endOfs;
  3365.         AEDesc   startObj;
  3366.         AEDesc   endObj;
  3367.         AEDesc   rangeDesc;
  3368.         long     startChar;
  3369.         long     endChar;
  3370.         Boolean  spotFlag;
  3371.             
  3372.         myErr = noErr;
  3373.         
  3374.         if (theWindow==nil) 
  3375.             return(noErr);
  3376.             
  3377.         selTextObj->dataHandle = nil;
  3378.         dMyDoc.dataHandle      = nil;
  3379.         startObj.dataHandle    = nil;
  3380.         endObj.dataHandle      = nil;
  3381.         
  3382.         /* 
  3383.             make the window object 
  3384.         */
  3385.         
  3386.         myErr = MakeWindowObj(theWindow, &dMyDoc);
  3387.             
  3388.         if (myErr==noErr)
  3389.             {
  3390.                 /* get the start and end of selection */
  3391.                 
  3392.                 startChar = selStart+1;    /* start counting obj's from 1, not 0 */
  3393.                 endChar   = selEnd;
  3394.                 spotFlag  = (selStart == selEnd);
  3395.                     
  3396.                 myErr = CreateOffsetDescriptor(startChar, &startOfs);
  3397.                             
  3398.                 if (myErr==noErr)
  3399.                     if (spotFlag)
  3400.                         myErr = CreateObjSpecifier(cSpot,
  3401.                                                                              &dMyDoc,
  3402.                                                                              formAbsolutePosition, 
  3403.                                                                              &startOfs, 
  3404.                                                                              true, 
  3405.                                                                              selTextObj);
  3406.                     else
  3407.                         {
  3408.                             /* not a spot - must represent as range */
  3409.                             /* make obj for start char */
  3410.                             
  3411.                             myErr = CreateObjSpecifier(cChar,
  3412.                                                        &dMyDoc,
  3413.                                                                                  formAbsolutePosition,
  3414.                                                                                  &startOfs,
  3415.                                                                                  false,
  3416.                                                                                  &startObj);
  3417.                             
  3418.                             if (myErr==noErr) 
  3419.                                 myErr = CreateOffsetDescriptor(endChar, &endOfs);
  3420.                             
  3421.                             if (myErr==noErr) 
  3422.                                 myErr = CreateObjSpecifier(cChar,
  3423.                                                                                      &dMyDoc,
  3424.                                                                                      formAbsolutePosition,
  3425.                                                                                      &endOfs,
  3426.                                                                                      false,
  3427.                                                                                      &endObj);
  3428.                             
  3429.                             if (myErr==noErr) 
  3430.                                 myErr = CreateRangeDescriptor(&startObj,
  3431.                                                                                             &endObj,
  3432.                                                                                             false,
  3433.                                                                                             &rangeDesc);
  3434.                             
  3435.                             if (myErr==noErr) 
  3436.                                 myErr = CreateObjSpecifier(cChar,
  3437.                                                                                      &dMyDoc,
  3438.                                                                                      formRange,
  3439.                                                                                      &rangeDesc,
  3440.                                                                                      true,
  3441.                                                                                      selTextObj);
  3442.                                                                                      
  3443.                             if (startObj.dataHandle)
  3444.                               ignoreErr = AEDisposeDesc(&startObj);
  3445.                                 
  3446.                             if (startOfs.dataHandle)
  3447.                               ignoreErr = AEDisposeDesc(&startOfs);
  3448.                                 
  3449.                             if (endObj.dataHandle)
  3450.                               ignoreErr = AEDisposeDesc(&endObj);
  3451.                                 
  3452.                             if (endOfs.dataHandle)
  3453.                               ignoreErr = AEDisposeDesc(&endOfs);
  3454.                         }
  3455.             }
  3456.             
  3457.         return(myErr);
  3458.     }
  3459.     
  3460. pascal OSErr MakeSelectedTextObj(WindowPtr theWindow,
  3461.                                                                  TEHandle  theTextEditHandle,
  3462.                                                                  AEDesc    *selTextObj)
  3463.     {
  3464.         return( MakeTextObj(theWindow,
  3465.                                                 (**theTextEditHandle).selStart,
  3466.                                                 (**theTextEditHandle).selEnd,
  3467.                                               selTextObj));
  3468.                                                 
  3469.     }    /* MakeSelectedTextObj */
  3470.  
  3471. enum editCommandType {
  3472. editCutCommand   = 1,
  3473. editCopyCommand  = 2,
  3474. editPasteCommand = 3,
  3475. editClearCommand = 4
  3476. };
  3477.  
  3478. typedef enum editCommandType editCommandType;
  3479.  
  3480. pascal void DoEditCommand(DPtr theDocument,editCommandType whatCommand)
  3481.     {
  3482.       OSErr         err;
  3483.       OSErr         forgetErr;
  3484.         AEAddressDesc ourAddress;
  3485.         AppleEvent    editCommandEvent;
  3486.         AppleEvent    ignoreReply;
  3487.         AEDesc        ourTextSelObj;
  3488.         AEEventID     theEventID;
  3489.         AEEventClass  theEventClass;
  3490.  
  3491.         /*
  3492.                 Initialise
  3493.         */
  3494.  
  3495.         ourAddress.dataHandle             = nil;
  3496.         ourTextSelObj.dataHandle         = nil;
  3497.         editCommandEvent.dataHandle = nil;
  3498.         ignoreReply.dataHandle             = nil;
  3499.         
  3500.         err = MakeSelfAddress(&ourAddress);
  3501.         
  3502.         /*
  3503.             Build an object to represent the current document's selection
  3504.         */
  3505.         err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &ourTextSelObj);
  3506.         
  3507.         if (err==noErr)
  3508.           {
  3509.                 switch (whatCommand) {
  3510.                     case  editCutCommand         : theEventID    = kAECut;
  3511.                                                                         theEventClass = kAEMiscStandards;
  3512.                                                                         break;
  3513.                     case  editCopyCommand     : theEventID    = kAECopy;
  3514.                                                                         theEventClass = kAEMiscStandards;
  3515.                                                                         break;
  3516.                     case  editPasteCommand     : theEventID    = kAEPaste;
  3517.                                                                         theEventClass = kAEMiscStandards;
  3518.                                                                         break;
  3519.                     case  editClearCommand     : theEventID    = kAEDelete;
  3520.                                                                         theEventClass = kAECoreSuite;
  3521.                                                                         break;
  3522.                 }
  3523.                     
  3524.                 err = AECreateAppleEvent( theEventClass, theEventID, &ourAddress, 0, 0, &editCommandEvent);    
  3525.  
  3526.                 /* add parameter */    
  3527.                 if (err==noErr) 
  3528.                     err = AEPutParamDesc(&editCommandEvent, keyDirectObject, &ourTextSelObj);
  3529.                     
  3530.                 /*and now Send the message*/
  3531.                 if (err==noErr)
  3532.                     err = AESend(&editCommandEvent,&ignoreReply,kAENoReply,kAEHighPriority,10000,nil, nil);
  3533.             }              
  3534.             
  3535.         /*
  3536.             Clean up
  3537.         */
  3538.         if (ourAddress.dataHandle) 
  3539.             forgetErr = AEDisposeDesc(&ourAddress);
  3540.         
  3541.         if (editCommandEvent.dataHandle) 
  3542.             forgetErr = AEDisposeDesc(&editCommandEvent);
  3543.         
  3544.         if (ignoreReply.dataHandle) 
  3545.             forgetErr = AEDisposeDesc(&ignoreReply);
  3546.             
  3547.         if (ourTextSelObj.dataHandle) 
  3548.             forgetErr = AEDisposeDesc(&ourTextSelObj);
  3549.             
  3550.     } /*DoEditCommand*/
  3551.     
  3552. pascal void IssueCutCommand(DPtr theDocument)
  3553.     {            
  3554.         DoEditCommand(theDocument, editCutCommand);
  3555.     } 
  3556.     
  3557. pascal void IssueCopyCommand(DPtr theDocument)
  3558.     {
  3559.         DoEditCommand(theDocument, editCopyCommand);
  3560.     }
  3561.     
  3562. pascal void IssuePasteCommand(DPtr theDocument)
  3563.     {
  3564.         DoEditCommand(theDocument, editPasteCommand);    
  3565.     }
  3566.     
  3567. pascal void IssueClearCommand(DPtr theDocument)
  3568.     {
  3569.         DoEditCommand(theDocument, editClearCommand);    
  3570.     }
  3571.     
  3572. pascal void IssueFontCommand(DPtr theDocument, short theItem)
  3573.     {
  3574.        Str255         name;
  3575.          AEDesc           strDesc;
  3576.          AEAddressDesc  theAddress;
  3577.          OSErr            err;
  3578.          AEDesc         selTextObj;
  3579.             
  3580.         err = MakeSelfAddress(&theAddress);
  3581.         
  3582.         err = MakeSelectedTextObj(theDocument->theWindow,
  3583.                                                             theDocument->theText,
  3584.                                                             &selTextObj);
  3585.         
  3586.         GetItem(myMenus[fontM], theItem, name);
  3587.         
  3588.         if (err==noErr)
  3589.             err  = AECreateDesc(typeChar,
  3590.                                                     (Ptr)&name[1],
  3591.                                                     name[0],
  3592.                                                     &strDesc);
  3593.         
  3594.         if (err==noErr)
  3595.             err  = SendAESetObjProp(&selTextObj, 
  3596.                                                             pFont,
  3597.                                                             &strDesc,
  3598.                                                             &theAddress);                                                            
  3599.     }
  3600.     
  3601. /*
  3602.     Window property routines
  3603. */
  3604.  
  3605. pascal void IssueZoomCommand(WindowPtr whichWindow, short whichPart)
  3606.     {
  3607.       Boolean       zoomBool;
  3608.         AEDesc        zoomDesc;
  3609.         AEAddressDesc selfAddr;
  3610.         AEDesc        frontWinObj;
  3611.         OSErr         err;
  3612.     
  3613.         err = MakeSelfAddress(&selfAddr);
  3614.         
  3615.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3616.         
  3617.         zoomBool = (whichPart==inZoomOut);
  3618.     
  3619.         err = AECreateDesc(typeBoolean,
  3620.                                              (Ptr)&zoomBool,
  3621.                                              sizeof(zoomBool),
  3622.                                              &zoomDesc);
  3623.                                                 
  3624.         err = SendAESetObjProp(&frontWinObj, 
  3625.                                                      pIsZoomed,
  3626.                                                      &zoomDesc,
  3627.                                                      &selfAddr);                                                            
  3628.     } /* IssueZoomCommand */
  3629.     
  3630. pascal void IssueCloseCommand(WindowPtr whichWindow)
  3631.     {
  3632.         AEAddressDesc  selfAddr;
  3633.         AEDesc         frontWinObj;
  3634.         OSErr          err;
  3635.         OSErr          ignoreErr;
  3636.         AppleEvent     closeCommandEvent;
  3637.         AppleEvent     ignoreReply;
  3638.     
  3639.         frontWinObj.dataHandle = nil;
  3640.         
  3641.         err = MakeSelfAddress(&selfAddr);
  3642.         
  3643.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3644.                                                             
  3645.         err = AECreateAppleEvent( kAECoreSuite, kAEClose, &selfAddr, 0, 0, &closeCommandEvent) ;                
  3646.         
  3647.         /* add parameter - the window to close */    
  3648.         if (err==noErr) 
  3649.             err = AEPutParamDesc(&closeCommandEvent, keyDirectObject, &frontWinObj);
  3650.             
  3651.         if (err==noErr) 
  3652.             err = AESend(&closeCommandEvent,&ignoreReply,kAENoReply+kAEAlwaysInteract,kAEHighPriority,10000,nil, nil);
  3653.         
  3654.         if (closeCommandEvent.dataHandle) 
  3655.             ignoreErr = AEDisposeDesc(&closeCommandEvent);
  3656.         
  3657.         if (selfAddr.dataHandle) 
  3658.             ignoreErr = AEDisposeDesc(&selfAddr);
  3659.             
  3660.         if (frontWinObj.dataHandle) 
  3661.             ignoreErr = AEDisposeDesc(&frontWinObj);
  3662.             
  3663.     } /* IssueCloseCommand */
  3664.     
  3665. pascal void IssueSizeWindow(WindowPtr whichWindow, short newHSize, short newVSize)
  3666.     {
  3667.       Rect          sizeRect;
  3668.         Rect          contentRect;
  3669.         short         edgeSize;
  3670.         AEDesc        sizeDesc;
  3671.         AEAddressDesc selfAddr;
  3672.         AEDesc        frontWinObj;
  3673.         OSErr         err;
  3674.     
  3675.         sizeRect    = (**(((WindowPeek)whichWindow)->strucRgn)).rgnBBox;
  3676.         contentRect = (**(((WindowPeek)whichWindow)->contRgn)).rgnBBox;
  3677.         
  3678.         edgeSize = sizeRect.right-sizeRect.left-(contentRect.right-contentRect.left);
  3679.         sizeRect.right = sizeRect.left+newHSize+edgeSize;
  3680.         
  3681.         edgeSize = sizeRect.bottom-sizeRect.top-(contentRect.bottom-contentRect.top);
  3682.         sizeRect.bottom = sizeRect.top+newVSize+edgeSize;
  3683.         
  3684.         err = MakeSelfAddress(&selfAddr);
  3685.         
  3686.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3687.         
  3688.         if (err==noErr)
  3689.             err = AECreateDesc(typeQDRectangle,
  3690.                                                  (Ptr)&sizeRect,
  3691.                                                  sizeof(sizeRect),
  3692.                                                  &sizeDesc);
  3693.         
  3694.         if (err==noErr)
  3695.             err  = SendAESetObjProp(&frontWinObj, 
  3696.                                                             pBounds,
  3697.                                                             &sizeDesc,
  3698.                                                             &selfAddr);                                                            
  3699.     } /*IssueSizeWindow*/
  3700.     
  3701. pascal void IssueMoveWindow(WindowPtr whichWindow, Rect sizeRect)
  3702.     {
  3703.         AEDesc        sizeDesc;
  3704.         AEAddressDesc selfAddr;
  3705.         AEDesc        frontWinObj;
  3706.         OSErr         err;
  3707.     
  3708.         err = MakeSelfAddress(&selfAddr);
  3709.         
  3710.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3711.             
  3712.         if (err==noErr)
  3713.             err = AECreateDesc(typeQDRectangle,
  3714.                                                  (Ptr)&sizeRect,
  3715.                                                  sizeof(sizeRect),
  3716.                                                  &sizeDesc);
  3717.         
  3718.         if (err==noErr)        
  3719.             err = SendAESetObjProp(&frontWinObj, 
  3720.                                                          pBounds,
  3721.                                                          &sizeDesc,
  3722.                                                          &selfAddr);                                                            
  3723.     } /*IssueMoveWindow*/
  3724.  
  3725. pascal void IssuePageSetupWindow(WindowPtr whichWindow, TPrint thePageSetup)
  3726.     {
  3727.         AEDesc        sizeDesc;
  3728.         AEAddressDesc selfAddr;
  3729.         AEDesc        frontWinObj;
  3730.         OSErr         err;
  3731.     
  3732.         err = MakeSelfAddress(&selfAddr);
  3733.         
  3734.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3735.         
  3736.         if (err==noErr)
  3737.             err = AECreateDesc(typeTPrint,
  3738.                                                  (Ptr)&thePageSetup,
  3739.                                                  sizeof(thePageSetup),
  3740.                                                  &sizeDesc);
  3741.                                                  
  3742.         if (err==noErr)
  3743.             err = SendAESetObjProp(&frontWinObj, 
  3744.                                                          pPageSetup,
  3745.                                                          &sizeDesc,
  3746.                                                          &selfAddr);                                                            
  3747.                                                      
  3748.     } /*IssuePageSetupWindow*/
  3749.     
  3750. pascal void IssueShowBorders(WindowPtr whichWindow, Boolean showBorders)
  3751.     {
  3752.         AEDesc        sizeDesc;
  3753.         AEAddressDesc selfAddr;
  3754.         AEDesc        frontWinObj;
  3755.         OSErr         err;
  3756.     
  3757.         err = MakeSelfAddress(&selfAddr);
  3758.         
  3759.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3760.         
  3761.         if (err==noErr)
  3762.             err = AECreateDesc(typeBoolean,
  3763.                                                  (Ptr)&showBorders,
  3764.                                                  sizeof(showBorders),
  3765.                                                  &sizeDesc);
  3766.         
  3767.         if (err==noErr)
  3768.             err = SendAESetObjProp(&frontWinObj, 
  3769.                                                          pShowBorders,
  3770.                                                          &sizeDesc,
  3771.                                                          &selfAddr);                                                            
  3772.     } /*IssueShowBorders*/
  3773.     
  3774. pascal void IssuePrintWindow(WindowPtr whichWindow)
  3775.     {
  3776.         AEAddressDesc selfAddr;
  3777.         AEDesc        frontWinObj;
  3778.         OSErr         err;
  3779.         OSErr         ignoreErr;
  3780.         AppleEvent    printCommandEvent;
  3781.         AppleEvent    ignoreReply;
  3782.     
  3783.         err = MakeSelfAddress(&selfAddr);
  3784.         
  3785.         err = MakeWindowObj(whichWindow, &frontWinObj);
  3786.                                                             
  3787.         err = AECreateAppleEvent(kCoreEventClass, kAEPrintDocuments, &selfAddr, 0, 0, &printCommandEvent) ;                
  3788.  
  3789.         /* 
  3790.             add parameter - the window to print
  3791.         */    
  3792.  
  3793.         if (err==noErr) 
  3794.             err = AEPutParamDesc(&printCommandEvent, keyDirectObject, &frontWinObj);
  3795.             
  3796.         if (err==noErr) 
  3797.             err = AESend(&printCommandEvent,&ignoreReply,kAENoReply+kAEAlwaysInteract,kAEHighPriority,10000,nil, nil);
  3798.     
  3799.       if (printCommandEvent.dataHandle)
  3800.             ignoreErr = AEDisposeDesc(&printCommandEvent);
  3801.         
  3802.         if (frontWinObj.dataHandle) 
  3803.             err = AEDisposeDesc(&frontWinObj);
  3804.             
  3805.         if (selfAddr.dataHandle) 
  3806.             err = AEDisposeDesc(&selfAddr);
  3807.             
  3808.     } /*IssuePrintWindow*/
  3809.     
  3810. pascal OSErr IssueAEOpenDoc(FSSpec myFSSpec)
  3811. /* send OpenDocs AppleEvent to myself, with a one-element list
  3812.   containing the given file spec
  3813.  
  3814.   NOTES : the core AEOpenDocs event is defined as taking a list of
  3815.           aliases (not file specs) as its direct parameter.  However,
  3816.             we can send the file spec instead and depend on AppleEvents'
  3817.             automatic coercion.  In fact, we don't really even have to put 
  3818.             in a list; AppleEvents will coerce a descriptor into a 1-element
  3819.             list if called for.  In this routine, though, we'll make the
  3820.             list for demonstration purposes.
  3821. */
  3822.  
  3823.  {
  3824.          AppleEvent    myAppleEvent;
  3825.         AppleEvent    defReply;
  3826.         AEDescList    docList;
  3827.         AEAddressDesc selfAddr;
  3828.         OSErr         myErr;
  3829.         OSErr         ignoreErr;
  3830.         
  3831.         myAppleEvent.dataHandle = nil;
  3832.         docList.dataHandle  = nil;
  3833.         selfAddr.dataHandle = nil;
  3834.         defReply.dataHandle = nil;
  3835.             
  3836.         /*
  3837.             Create empty list and add one file spec
  3838.         */
  3839.         myErr = AECreateList(nil,0,false, &docList);
  3840.         
  3841.         if (myErr==noErr) 
  3842.             myErr = AEPutPtr(&docList,1,typeFSS,(Ptr)&myFSSpec,sizeof(myFSSpec));
  3843.             
  3844.         /*
  3845.             Create a self address to send it to
  3846.         */
  3847.         if (myErr==noErr) 
  3848.             myErr = MakeSelfAddress(&selfAddr);
  3849.             
  3850.         if (myErr==noErr) 
  3851.             myErr = AECreateAppleEvent(kCoreEventClass,
  3852.                                                                  kAEOpenDocuments,
  3853.                                                                  &selfAddr,
  3854.                                                                  kAutoGenerateReturnID,
  3855.                                                                  kAnyTransactionID,
  3856.                                                                  &myAppleEvent);
  3857.     
  3858.         /* 
  3859.             Put Params into our event and send it
  3860.         */
  3861.         if (myErr == noErr) 
  3862.             myErr = AEPutParamDesc(&myAppleEvent,
  3863.                                                          keyDirectObject,
  3864.                                                          &docList);
  3865.     
  3866.         myErr = AESend(&myAppleEvent,
  3867.                                      &defReply,
  3868.                                      kAENoReply+kAEAlwaysInteract,
  3869.                                      kAENormalPriority,
  3870.                                      kAEDefaultTimeout,
  3871.                                      nil,
  3872.                                      nil);
  3873.             
  3874.         if (selfAddr.dataHandle) 
  3875.             ignoreErr = AEDisposeDesc(&selfAddr);
  3876.             
  3877.         if (myAppleEvent.dataHandle) 
  3878.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  3879.             
  3880.         if (docList.dataHandle) 
  3881.             ignoreErr = AEDisposeDesc(&docList);
  3882.             
  3883.         return(myErr);
  3884.         
  3885.     }    /* IssueAEOpenDoc */
  3886.  
  3887. pascal void IssueAENewWindow(void)
  3888. /* 
  3889.     send the New Element event to myself with a null container
  3890. */
  3891.     {
  3892.       AppleEvent    myAppleEvent;
  3893.         AppleEvent    defReply;
  3894.         AEAddressDesc selfAddr;
  3895.         OSErr         myErr;
  3896.         OSErr         ignoreErr;
  3897.         DescType      elemClass;
  3898.         
  3899.         myAppleEvent.dataHandle = nil;
  3900.         
  3901.         /*
  3902.             Create the address of us
  3903.         */
  3904.         
  3905.         myErr = MakeSelfAddress(&selfAddr);
  3906.         
  3907.         /*
  3908.             create event 
  3909.         */
  3910.         
  3911.         myErr = AECreateAppleEvent(kAECoreSuite,
  3912.                                                              kAECreateElement,
  3913.                                                              &selfAddr,
  3914.                                                              kAutoGenerateReturnID,
  3915.                                                              kAnyTransactionID,
  3916.                                                              &myAppleEvent);
  3917.         
  3918.         /*
  3919.             attach desired class of new element
  3920.         */
  3921.         
  3922.         elemClass = cWindow;
  3923.         
  3924.         if (myErr == noErr) 
  3925.             myErr = AEPutParamPtr(&myAppleEvent,
  3926.                                                       keyAEObjectClass,
  3927.                                                         typeType,
  3928.                                                         (Ptr)&elemClass,
  3929.                                                         sizeof(elemClass));
  3930.             
  3931.         /*
  3932.             send the event 
  3933.         */
  3934.         
  3935.         if (myErr == noErr) 
  3936.             myErr = AESend(&myAppleEvent,
  3937.                                          &defReply,
  3938.                                          kAENoReply+kAENeverInteract,
  3939.                                          kAENormalPriority,
  3940.                                          kAEDefaultTimeout,
  3941.                                          nil,
  3942.                                          nil);
  3943.         /*
  3944.             Clean up - reply never created so don't throw away
  3945.         */
  3946.         if (selfAddr.dataHandle) 
  3947.             ignoreErr = AEDisposeDesc(&selfAddr);
  3948.             
  3949.         if (myAppleEvent.dataHandle) 
  3950.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  3951.                     
  3952.     }    /* IssueAENewWindow */
  3953.  
  3954. pascal OSErr IssueSaveCommand(WindowPtr theWindow,
  3955.                               FSSpecPtr where)
  3956. /*
  3957.     send an AppleEvent Save Event to myself
  3958. */
  3959.  
  3960. {
  3961.     AEDesc        windowObj;
  3962.     AppleEvent    myAppleEvent;
  3963.     AppleEvent    defReply;
  3964.     OSErr         myErr;
  3965.     OSErr         ignoreErr;
  3966.     AEAddressDesc selfAddr;
  3967.         
  3968.     windowObj.dataHandle = nil;
  3969.     myAppleEvent.dataHandle = nil;
  3970.     
  3971.     myErr = MakeWindowObj(theWindow, &windowObj);
  3972.             
  3973.     if (myErr==noErr) 
  3974.         myErr = MakeSelfAddress(&selfAddr);
  3975.         
  3976.   /*
  3977.         Build event
  3978.     */
  3979.     
  3980.   if (myErr == noErr) 
  3981.         myErr = AECreateAppleEvent(kAECoreSuite,
  3982.                                                              kAESave,
  3983.                                                              &selfAddr,
  3984.                                                              kAutoGenerateReturnID,
  3985.                                                              kAnyTransactionID,
  3986.                                                              &myAppleEvent);
  3987.       
  3988.   /*
  3989.         say which window
  3990.     */
  3991.     
  3992.   if (myErr==noErr) 
  3993.         myErr = AEPutParamDesc(&myAppleEvent,
  3994.                                                      keyDirectObject,
  3995.                                                      &windowObj);
  3996.   
  3997.   /*
  3998.         add optional file param if we need to
  3999.     */
  4000.     
  4001.   if (where) 
  4002.         if (myErr==noErr) 
  4003.             myErr = AEPutParamPtr(&myAppleEvent,
  4004.                                                         keyAEDestination,
  4005.                                                         typeFSS,
  4006.                                                         (Ptr)where,
  4007.                                                         sizeof(FSSpec));
  4008.         
  4009.   /*
  4010.         send the event
  4011.     */
  4012.   if (myErr==noErr) 
  4013.         myErr  = AESend(&myAppleEvent,
  4014.                                         &defReply,
  4015.                                         kAENoReply+kAENeverInteract,
  4016.                                         kAENormalPriority,
  4017.                                         kAEDefaultTimeout,
  4018.                                         nil,
  4019.                                         nil);
  4020.       
  4021.     if (selfAddr.dataHandle) 
  4022.         ignoreErr = AEDisposeDesc(&selfAddr);
  4023.       
  4024.     if (windowObj.dataHandle) 
  4025.         ignoreErr = AEDisposeDesc(&windowObj);
  4026.         
  4027.     if (myAppleEvent.dataHandle) 
  4028.         myErr = AEDisposeDesc(&myAppleEvent);
  4029.         
  4030.     return(myErr);
  4031. }    /* IssueSaveCommand */
  4032.  
  4033. pascal OSErr IssueRevertCommand(WindowPtr theWindow)
  4034. /*
  4035.     send an AppleEvent Revert Event to myself
  4036. */
  4037.  
  4038.     {
  4039.         AEDesc        windowObj;
  4040.         AppleEvent    myAppleEvent;
  4041.         AppleEvent    defReply;
  4042.         OSErr         myErr;
  4043.         OSErr         ignoreErr;
  4044.         AEAddressDesc selfAddr;
  4045.         
  4046.         windowObj.dataHandle = nil;
  4047.         myAppleEvent.dataHandle = nil;
  4048.         
  4049.         myErr = MakeWindowObj(theWindow, &windowObj);
  4050.                     
  4051.         if (myErr==noErr) 
  4052.             myErr = MakeSelfAddress(&selfAddr);
  4053.             
  4054.         /*
  4055.             Build event
  4056.         */
  4057.         
  4058.         if (myErr == noErr) 
  4059.             myErr  = AECreateAppleEvent(kAEMiscStandards,
  4060.                                                                     kAERevert,
  4061.                                                                     &selfAddr,
  4062.                                                                     kAutoGenerateReturnID,
  4063.                                                                     kAnyTransactionID,
  4064.                                                                     &myAppleEvent);
  4065.         /*
  4066.             say which window
  4067.         */
  4068.         
  4069.         if (myErr == noErr) 
  4070.             myErr = AEPutParamDesc(&myAppleEvent,
  4071.                                                          keyDirectObject,
  4072.                                                          &windowObj);
  4073.         /*
  4074.             send the event
  4075.         */
  4076.         if (myErr==noErr) 
  4077.             myErr  = AESend(&myAppleEvent,
  4078.                                             &defReply,
  4079.                                             kAENoReply+kAENeverInteract,
  4080.                                             kAENormalPriority,
  4081.                                             kAEDefaultTimeout,
  4082.                                             nil,
  4083.                                             nil);
  4084.             
  4085.         if (windowObj.dataHandle) 
  4086.             ignoreErr = AEDisposeDesc(&windowObj);
  4087.             
  4088.         if (myAppleEvent.dataHandle) 
  4089.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  4090.             
  4091.         if (selfAddr.dataHandle) 
  4092.             ignoreErr = AEDisposeDesc(&selfAddr);
  4093.             
  4094.         return(myErr);
  4095.     }    /* IssueRevertCommand */
  4096.  
  4097. /*
  4098.     Name : IssueQuitCommand
  4099.     Purpose : Sends self a Quit AppleEvent
  4100. */
  4101. pascal OSErr IssueQuitCommand(void)
  4102.     {
  4103.         AppleEvent    myAppleEvent;
  4104.         AppleEvent    defReply;
  4105.         OSErr         myErr;
  4106.         OSErr         ignoreErr;
  4107.         AEAddressDesc selfAddr;
  4108.         DescType      mySaveOpt;
  4109.         
  4110.         myAppleEvent.dataHandle = nil;
  4111.         selfAddr.dataHandle     = nil;
  4112.                             
  4113.         myErr = MakeSelfAddress(&selfAddr);
  4114.             
  4115.         /*
  4116.             Build event
  4117.         */
  4118.         
  4119.         if (myErr == noErr) 
  4120.             myErr  = AECreateAppleEvent(kCoreEventClass,
  4121.                                                                     kAEQuitApplication,
  4122.                                                                     &selfAddr,
  4123.                                                                     kAutoGenerateReturnID,
  4124.                                                                     kAnyTransactionID,
  4125.                                                                     &myAppleEvent);
  4126.         /*
  4127.             say which save option
  4128.         */
  4129.         
  4130.         mySaveOpt = kAEAsk;
  4131.         
  4132.         if (myErr == noErr) 
  4133.             myErr = AEPutParamPtr(&myAppleEvent,
  4134.                                                         keyAESaveOptions,
  4135.                                                         typeEnumerated,
  4136.                                                         (Ptr)&mySaveOpt,
  4137.                                                         sizeof(mySaveOpt));
  4138.         /*
  4139.             send the event
  4140.         */
  4141.         if (myErr==noErr) 
  4142.             myErr  = AESend(&myAppleEvent,
  4143.                                             &defReply,
  4144.                                             kAENoReply+kAEAlwaysInteract,
  4145.                                             kAENormalPriority,
  4146.                                             kAEDefaultTimeout,
  4147.                                             nil,
  4148.                                             nil);
  4149.                         
  4150.         if (myAppleEvent.dataHandle) 
  4151.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  4152.             
  4153.         if (selfAddr.dataHandle) 
  4154.             ignoreErr = AEDisposeDesc(&selfAddr);
  4155.             
  4156.         return(myErr);
  4157.     }    /* IssueQuitCommand */
  4158.  
  4159. /*
  4160.      Name :IssueCreatePublisher
  4161.      Purpose :Interact with user to get Publisher info
  4162.                         and the IssueAECommand to Publish currect selection
  4163. */
  4164. pascal void IssueCreatePublisher(DPtr whichDoc)
  4165.     {
  4166.         AEAddressDesc selfAddr;
  4167.         AEDesc        selTextObj;
  4168.         OSErr         err;
  4169.         OSErr         ignoreErr;
  4170.         AppleEvent    publishCommandEvent;
  4171.         AppleEvent    ignoreReply;
  4172.     
  4173.       publishCommandEvent.dataHandle = nil;
  4174.         selfAddr.dataHandle = nil;
  4175.         selTextObj.dataHandle = nil;
  4176.         
  4177.         err = MakeSelfAddress(&selfAddr);
  4178.         
  4179.         if (err==noErr)
  4180.             err = MakeSelectedTextObj(whichDoc->theWindow, whichDoc->theText, &selTextObj);
  4181.                                                             
  4182.         err = AECreateAppleEvent( kAEMiscStandards,
  4183.                                   kAECreatePublisher,
  4184.                                                             &selfAddr,
  4185.                                                             0,
  4186.                                                             0,
  4187.                                                             &publishCommandEvent) ;                
  4188.  
  4189.         /* 
  4190.             add parameter - the text to publish
  4191.         */    
  4192.  
  4193.         if (err==noErr) 
  4194.             err = AEPutParamDesc(&publishCommandEvent, keyDirectObject, &selTextObj);
  4195.             
  4196.         if (err==noErr) 
  4197.             err = AESend(&publishCommandEvent,&ignoreReply,kAENoReply+kAEAlwaysInteract,kAEHighPriority,10000,nil, nil);
  4198.     
  4199.       if (publishCommandEvent.dataHandle)
  4200.             ignoreErr = AEDisposeDesc(&publishCommandEvent);
  4201.         
  4202.         if (selTextObj.dataHandle) 
  4203.             ignoreErr = AEDisposeDesc(&selTextObj);
  4204.             
  4205.         if (selfAddr.dataHandle) 
  4206.             ignoreErr = AEDisposeDesc(&selfAddr);
  4207.             
  4208.     } /*IssueCreatePublisher*/
  4209.     
  4210. #define kOK 1
  4211. #define kCancel 2
  4212. #define kOtherSize 4
  4213. #define kOutlineItem 5
  4214.  
  4215. pascal Boolean PoseSizeDialog(long *whatSize)
  4216.   {
  4217.       GrafPtr   savedPort;
  4218.     DialogPtr aDialog;
  4219.       Str255    aString;
  4220.       short     itemHit;
  4221.  
  4222.         GetPort(&savedPort);
  4223.         aDialog = GetNewDialog(1004, nil, (WindowPtr)-1);
  4224.         ShowWindow(aDialog);
  4225.         SetPort(aDialog);
  4226.         
  4227.         AdornDefaultButton(aDialog, kOutlineItem);
  4228.          
  4229.         /*set the edittext button to contain the right size*/
  4230.         NumToString(*whatSize, aString);
  4231.         SetText(aDialog, kOtherSize, aString);
  4232.      
  4233.         do {
  4234.             ModalDialog(nil, &itemHit);
  4235.         } while ((itemHit!=kOK) && (itemHit!=kCancel));
  4236.         
  4237.         if (itemHit == kOK) 
  4238.             RetrieveText(aDialog, kOtherSize, aString);
  4239.     
  4240.         DisposDialog(aDialog);
  4241.         SetPort(savedPort);
  4242.         
  4243.         if (itemHit == kOK) 
  4244.          {
  4245.              /*set the new size of the text*/
  4246.              StringToNum(aString, whatSize);
  4247.              if ((*whatSize<1) || (*whatSize>2000)) 
  4248.                  *whatSize = 12;
  4249.          }
  4250.         return(itemHit == kOK);
  4251.     }
  4252.     
  4253. pascal void IssueSizeCommand(DPtr theDocument,short theItem)
  4254.     {
  4255.         Str255        name;
  4256.         AEDesc        sizeDesc;
  4257.         AEAddressDesc theAddress;
  4258.         OSErr         err;
  4259.         AEDesc        selTextObj;
  4260.         
  4261.         /*
  4262.             Vars to do with menu processing
  4263.         */
  4264.         short     lastSize;
  4265.         short     upItem;
  4266.         short     downItem;
  4267.         short     otherItem;
  4268.         long      theSize;
  4269.         TextStyle theStyle;
  4270.         short     lineHeight;
  4271.         short     fontAscent;
  4272.             
  4273.         err = MakeSelfAddress(&theAddress);
  4274.         
  4275.         err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);        
  4276.  
  4277.      /*check if the item is on the Size menu*/
  4278.      /*remembering that we can add and delete items from it*/
  4279.      lastSize  = CountMItems(myMenus[sizeM]) - 5;
  4280.      upItem    = lastSize + 2;
  4281.      downItem  = upItem + 1;
  4282.      otherItem = downItem + 2;
  4283.       
  4284.      TEGetStyle((**(theDocument->theText)).selStart,
  4285.                 &theStyle,
  4286.                             &lineHeight,
  4287.                             &fontAscent,
  4288.                             theDocument->theText);
  4289.         
  4290.      GetItem(myMenus[sizeM], theItem, name);
  4291.      
  4292.      if (theItem <= lastSize)
  4293.        {
  4294.          GetItem(myMenus[sizeM], theItem, name);
  4295.        StringToNum(name, &theSize);
  4296.        }
  4297.      else
  4298.        if (theItem == upItem)  
  4299.             theSize = theStyle.tsSize+1;
  4300.          else
  4301.             if (theItem == downItem)
  4302.                       theSize = theStyle.tsSize-1;
  4303.                  else 
  4304.                     if (theItem == otherItem) 
  4305.                         {
  4306.                           theSize = theStyle.tsSize;
  4307.                             if (!PoseSizeDialog(&theSize))
  4308.                                 return;
  4309.                         }
  4310.  
  4311.         if (err==noErr)
  4312.           err = CreateOffsetDescriptor(theSize, &sizeDesc);
  4313.             
  4314.         if (err==noErr)
  4315.             err = SendAESetObjProp(&selTextObj, 
  4316.                                                          pPointSize,
  4317.                                                          &sizeDesc,
  4318.                                                          &theAddress);
  4319.                                                      
  4320.     } /*IssueSizeCommand*/
  4321.     
  4322. pascal void IssueStyleCommand(DPtr theDocument, short theItem)
  4323.     {
  4324.         Style         theFace;
  4325.         OSErr         err;
  4326.         AEDesc        result;
  4327.         AEAddressDesc selfAddr;
  4328.         AEDesc        selTextObj;
  4329.         TextStyle     theStyle;
  4330.         short         lineHeight;
  4331.         short         fontAscent;
  4332.             
  4333.         TEGetStyle((**(theDocument->theText)).selStart,
  4334.                    &theStyle,
  4335.                              &lineHeight,
  4336.                              &fontAscent,
  4337.                              theDocument->theText);
  4338.         
  4339.         theFace = 0;
  4340.     switch (theItem) {
  4341.             case cPlain              : theFace = 0;
  4342.                                 break;
  4343.             case cBold                : theFace = bold;
  4344.                                 break;
  4345.             case cItalic            : theFace = italic;
  4346.                                 break;
  4347.             case cUnderline      : theFace = underline;
  4348.                                 break;
  4349.             case cOutline          : theFace = outline;
  4350.                                 break;
  4351.             case cShadow            : theFace = shadow;
  4352.                                 break;
  4353.             case cCondense        : theFace = condense;
  4354.                                 break;
  4355.             case cExtend            : theFace = extend;
  4356.                                 break;
  4357.     } /*of case */
  4358.  
  4359.     if (theFace==0)
  4360.             err = BuildTypeTextStylesDesc(0, bold+italic+underline+outline+shadow+condense+extend, &result);
  4361.     else
  4362.       if (theFace & theStyle.tsFace)
  4363.               err = BuildTypeTextStylesDesc(0, theFace, &result);
  4364.             else
  4365.               err = BuildTypeTextStylesDesc(theFace, 0, &result);
  4366.         
  4367.         err = MakeSelfAddress(&selfAddr);
  4368.         
  4369.         if (err==noErr)
  4370.             err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);
  4371.         
  4372.         if (err==noErr)
  4373.             err = SendAESetObjProp(&selTextObj, 
  4374.                                                          pTextStyles,
  4375.                                                          &result,
  4376.                                                          &selfAddr);
  4377.         
  4378.     } /* IssueStyleCommand */
  4379.     
  4380. enum {
  4381.  
  4382. ETX = 0x03, /* Enter key on keyboard or keypad */
  4383. BS  = 0x08, /* Backspace key on keyboard       */
  4384. HT  = 0x09, /* Tab key on keyboard             */
  4385. CR  = 0x0D, /* Return key on keyboard          */
  4386. ESC = 0x1B, /* Clear key on keypad             */
  4387. FS  = 0x1C, /* Left arrow key on keypad        */
  4388. GS  = 0x1D, /* Right arrow key on keypad       */
  4389. RS  = 0x1E, /* Up arrow key on keypad          */
  4390. US  = 0x1F  /* Down arrow key on keypad        */
  4391. };
  4392.  
  4393. pascal OSErr IssueSetDataObjToBufferContents(const AEDesc * theObj)
  4394.     { 
  4395.       OSErr         myErr;
  4396.         OSErr                    ignoreErr;
  4397.         AEAddressDesc theAddress;
  4398.         AppleEvent    myAppleEvent;
  4399.         AppleEvent    defReply;
  4400.  
  4401.         myErr = MakeSelfAddress(&theAddress);
  4402.         
  4403.         /* create event */
  4404.         
  4405.         if (myErr==noErr)
  4406.             myErr = AECreateAppleEvent(kAECoreSuite,
  4407.                                                                  kAESetData,
  4408.                                                                  &theAddress,
  4409.                                                                  0,
  4410.                                                                  0,
  4411.                                                                  &myAppleEvent);
  4412.             
  4413.         /* add prop obj spec to the event */
  4414.         
  4415.         if (myErr==noErr)
  4416.             myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, theObj);
  4417.         
  4418.         /* add prop data to the event */
  4419.         
  4420.         if (myErr==noErr)
  4421.             myErr = AEPutParamPtr(&myAppleEvent,
  4422.                                                         keyAEData,
  4423.                                                         typeChar,
  4424.                                                         (Ptr)gTypingBuffer,
  4425.                                                         gCharsInBuffer);
  4426.                                                         
  4427.         /* send event */
  4428.         
  4429.         if (myErr==noErr)
  4430.          if (gRecordingImplemented)
  4431.              myErr = AESend(&myAppleEvent,
  4432.                                          &defReply,
  4433.                                          kAENoReply+kAEDontExecute,
  4434.                                          kAENormalPriority,
  4435.                                          kAEDefaultTimeout,
  4436.                                          nil,
  4437.                                          nil);
  4438.  
  4439.         if (theAddress.dataHandle)
  4440.             ignoreErr = AEDisposeDesc(&theAddress);
  4441.             
  4442.         if (myAppleEvent.dataHandle)
  4443.             ignoreErr = AEDisposeDesc(&myAppleEvent);
  4444.             
  4445.         return(myErr);
  4446.     }
  4447.  
  4448. pascal void AddKeyToTypingBuffer(DPtr theDocument, char theKey)
  4449.     {
  4450.         OSErr myErr;
  4451.         OSErr ignoreErr;
  4452.         
  4453.         if (theKey==BS || theKey==FS || theKey==GS || theKey==RS || theKey==US)
  4454.             {
  4455.                 FlushAndRecordTypingBuffer();
  4456.                 if (theKey==BS)
  4457.                     {
  4458.                         if ((**theDocument->theText).selStart!=(**theDocument->theText).selEnd)
  4459.                             {
  4460.                                 myErr = MakeTextObj(theDocument->theWindow,
  4461.                                                                         (**theDocument->theText).selStart,
  4462.                                                                         (**theDocument->theText).selEnd,
  4463.                                                                         &gTypingTargetObject);
  4464.                             }
  4465.                         else
  4466.                             {
  4467.                                 myErr = MakeTextObj(theDocument->theWindow,
  4468.                                                                         (**theDocument->theText).selStart-1,
  4469.                                                                         (**theDocument->theText).selStart,
  4470.                                                                         &gTypingTargetObject);
  4471.                             }
  4472.                             
  4473.                     myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  4474.                         
  4475.                         ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  4476.                         
  4477.                         gTypingTargetObject.dataHandle = nil;
  4478.                     }
  4479.             }
  4480.         else
  4481.             {
  4482.                 if (gCharsInBuffer==0)
  4483.                     myErr = MakeSelectedTextObj(theDocument->theWindow,
  4484.                                                                             theDocument->theText,
  4485.                                                                             &gTypingTargetObject);
  4486.  
  4487.                 gTypingBuffer[gCharsInBuffer++] = theKey;
  4488.             }
  4489.           
  4490.     }
  4491.     
  4492. pascal void FlushAndRecordTypingBuffer(void)
  4493.     { 
  4494.       OSErr  myErr;
  4495.         OSErr  ignoreErr;
  4496.  
  4497.         if (gCharsInBuffer != 0)
  4498.             {
  4499.                 myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  4500.                 
  4501.                 if (gTypingTargetObject.dataHandle)
  4502.                     ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  4503.             }
  4504.             
  4505.         gCharsInBuffer = 0;
  4506.         gTypingTargetObject.dataHandle = 0;
  4507.     }
  4508.     
  4509. /*****************************************************************************/
  4510. /*
  4511.     Object Accessors
  4512. */
  4513.     
  4514. pascal OSErr WindowFromNullAccessor(DescType      wantClass,
  4515.                                                                     const AEDesc  *container,
  4516.                                                                         DescType      containerClass,
  4517.                                                                         DescType      form, 
  4518.                                                                         const AEDesc  *selectionData,
  4519.                                                                         AEDesc        *value,
  4520.                                                                         long          theRefCon)
  4521.     {
  4522. #pragma unused (container,theRefCon)
  4523.     
  4524.         OSErr       myErr;
  4525.         Str255      nameStr;
  4526.         WindowToken theWindow;
  4527.         short       index;
  4528.         AEDesc      resultDesc;
  4529.         
  4530.         myErr = errAEBadKeyForm;    /* or whatever */
  4531.         
  4532.         value->dataHandle     = nil;
  4533.         resultDesc.dataHandle = nil;
  4534.         
  4535.         /* 
  4536.             should only be called with wantClass = cWindow and
  4537.             with containerClass = typeNull or typeMyAppl.
  4538.             Currently accept as either formName or formAbsolutePosition
  4539.         */
  4540.         
  4541.         if ((wantClass != cWindow) || 
  4542.               ((containerClass != typeNull) && (containerClass != typeMyAppl)) ||
  4543.                !((form == formName) || (form == formAbsolutePosition)))
  4544.             return(errAEWrongDataType);
  4545.         
  4546.         if (form == formName)
  4547.             {
  4548.                 myErr     = GetPStringFromDescriptor(selectionData, (char *)nameStr);
  4549.                 theWindow = WindowNameToWindowPtr(nameStr);
  4550.             }
  4551.         
  4552.         if (form == formAbsolutePosition)
  4553.             {
  4554.                 myErr         = GetIntegerFromDescriptor(selectionData, &index);
  4555.                 
  4556.                 if (index<0)
  4557.                     index = CountWindows()+index+1;
  4558.                     
  4559.                 theWindow = GetWindowPtrOfNthWindow(index);
  4560.             }
  4561.             
  4562.         if (myErr == noErr)
  4563.             myErr = AECreateDesc(typeMyWndw, (Ptr)&theWindow, sizeof(theWindow), value);
  4564.                 
  4565.         return(myErr);
  4566.     }    /* WindowFromNullAccessor */
  4567.  
  4568. pascal OSErr ApplicationFromNullAccessor(DescType      wantClass,
  4569.                                                                                  const AEDesc  *container,
  4570.                                                                                  DescType      containerClass,
  4571.                                                                                  DescType      form, 
  4572.                                                                                  const AEDesc  *selectionData,
  4573.                                                                                  AEDesc        *value,
  4574.                                                                                  long          theRefCon)
  4575.     {
  4576. #pragma unused (container,selectionData,theRefCon)
  4577.  
  4578.         OSErr    myErr;
  4579.         appToken theApp;
  4580.         AEDesc   resultDesc;
  4581.         
  4582.         value->dataHandle     = nil;
  4583.         resultDesc.dataHandle = nil;
  4584.         
  4585.         /* 
  4586.             should only be called with wantClass = cWindow and
  4587.             with containerClass = typeNull.
  4588.             Currently accept as either formName or formAbsolutePosition
  4589.         */
  4590.         
  4591.         if ((wantClass != cApplication) || (containerClass != typeNull) ||
  4592.               !((form == formName) || (form == formAbsolutePosition)))
  4593.             return(errAEWrongDataType);
  4594.         
  4595.         if ((form == formName) || (form == formAbsolutePosition))
  4596.             {
  4597.                 theApp.highLongOfPSN = 0;
  4598.                 theApp.lowLongOfPSN  = kCurrentProcess;
  4599.             }
  4600.             
  4601.         myErr = AECreateDesc(typeMyAppl, (Ptr)&theApp, sizeof(theApp), value);
  4602.                 
  4603.         return(myErr);
  4604.     }    /* ApplicationFromNullAccessor */
  4605.  
  4606. pascal void MoveToNonSpace(short *start, short limit, charsHandle myChars)
  4607.     /*
  4608.         Treats space,comma, full stop, ; and : as space chars
  4609.     */
  4610.     { 
  4611.         short x;
  4612.  
  4613.         while (*start<=limit) {
  4614.           x = (**myChars)[*start];
  4615.             if ( x == ' ' ||
  4616.                  x == ',' ||
  4617.                  x == '.' ||
  4618.                  x == ',' ||
  4619.                  x == ':' ||
  4620.                  x == 10  ||
  4621.                  x == 13 )
  4622.                 (*start) +=1;
  4623.             else
  4624.                 return;
  4625.         }
  4626.     }
  4627.     
  4628. pascal void MoveToSpace(short *start, short limit, charsHandle myChars)
  4629.     /*
  4630.         Treats space,comma, full stop, ; and : as space chars
  4631.     */
  4632.     { 
  4633.       short x;
  4634.         
  4635.         while (*start<=limit) {
  4636.           x = (**myChars)[*start];
  4637.             if ( x != ' ' &&
  4638.                  x != ',' &&
  4639.                  x != '.' &&
  4640.                  x != ',' &&
  4641.                  x != ':' &&
  4642.                  x != 10  &&
  4643.                  x != 13 )
  4644.                 (*start) +=1;
  4645.             else
  4646.                 return;
  4647.         }
  4648.     }
  4649.  
  4650. pascal short CountWords(TEHandle inTextHandle, short startAt, short forHowManyChars)
  4651.     {
  4652.         charsHandle myChars;
  4653.         short       start;
  4654.         short       limit;
  4655.         short       myWords;
  4656.             
  4657.         myChars  = (charsHandle)(**inTextHandle).hText;
  4658.         limit    = startAt+forHowManyChars-1;
  4659.         start    = startAt;
  4660.         myWords  = 0;
  4661.         MoveToNonSpace(&start, limit, myChars);
  4662.         while (start<=limit) {
  4663.                 myWords++;
  4664.                 MoveToSpace(&start, limit, myChars);
  4665.                 MoveToNonSpace(&start, limit, myChars);
  4666.             }
  4667.         return(myWords);
  4668.     } /* CountWords */
  4669.     
  4670. pascal void GetNthWordInfo(short    whichWord,
  4671.                                                    TEHandle inTextHandle,
  4672.                                                    short    *wordStartChar,
  4673.                                                    short    *wordLength)
  4674.     /*
  4675.         On entry:    wordStartChar is start of char range to count in
  4676.                             wordLength is number of chars to consider
  4677.                             
  4678.         On Exit : wordStartChar is start of requested word
  4679.                             wordLength is number of chars in word
  4680.     */
  4681.     {
  4682.         charsHandle myChars;
  4683.         short       start;
  4684.         short       limit;
  4685.             
  4686.         myChars  = (charsHandle)(**inTextHandle).hText;
  4687.         limit    = *wordStartChar + *wordLength-1;
  4688.         start    = *wordStartChar;
  4689.         MoveToNonSpace(&start, limit, myChars);
  4690.         while ((start<=limit) && (whichWord>0)) {
  4691.  
  4692.                 whichWord       = whichWord-1;
  4693.                 *wordStartChar  = start;
  4694.                 MoveToSpace(&start, limit, myChars);
  4695.                 *wordLength     = start- *wordStartChar;
  4696.                     
  4697.                 MoveToNonSpace(&start, limit, myChars);
  4698.             }
  4699.     } /* GetNthWordInfo */
  4700.     
  4701. pascal void GetWordInfo(short    whichWord,
  4702.                                               TEHandle inTextHandle,
  4703.                                               short    *wordStartChar,
  4704.                                               short    *wordLength)
  4705.     /*
  4706.         On wordStartChar entry is start of char range to count in
  4707.                             wordLength is number of chars to consider
  4708.                             
  4709.         On Exit : wordStartChar is start of requested word
  4710.                             wordLength is number of chars in word
  4711.     */
  4712.     {
  4713.         short noOfWords;
  4714.                     
  4715.         noOfWords = CountWords(inTextHandle, *wordStartChar, *wordLength);
  4716.         
  4717.         if (whichWord<0)
  4718.             whichWord = noOfWords + whichWord + 1;
  4719.             
  4720.         if (whichWord>noOfWords)
  4721.             {
  4722.                 *wordStartChar = *wordStartChar+*wordLength;
  4723.                 *wordLength    = 0;
  4724.             }
  4725.         else
  4726.             GetNthWordInfo(whichWord, inTextHandle, wordStartChar, wordLength);
  4727.     }
  4728.     
  4729. pascal short CountLines(TEHandle inTextHandle)
  4730.     {
  4731.         /*
  4732.             CountLines makes use of info in TERec
  4733.         */
  4734.         return((**inTextHandle).nLines);
  4735.     }
  4736.     
  4737. pascal short LineOfOffset(TEHandle theHTE, short charOffset)
  4738.     {
  4739.         short n;
  4740.     
  4741.         n = (**theHTE).nLines;
  4742.         
  4743.         while (((**theHTE).lineStarts[n-1]>charOffset) &&
  4744.                      (n>0))
  4745.              n--;
  4746.             
  4747.         return(n);
  4748.     } /* LineOfOffset */
  4749.  
  4750. pascal void GetLineInfo(short    whichLine,
  4751.                                                 TEHandle inTextHandle,
  4752.                                               short    *lineStartChar,
  4753.                                               short    *lineLength)
  4754.     {
  4755.         short       noOfLines;
  4756.         charsHandle myChars;
  4757.             
  4758.         /* Addition of lines within text object */
  4759.         short       lineOfStart;
  4760.         short       lineOfEnd;
  4761.         
  4762.         lineOfStart = LineOfOffset(inTextHandle, *lineStartChar);
  4763.         lineOfEnd   = LineOfOffset(inTextHandle, *lineStartChar+*lineLength-1);
  4764.             
  4765.         myChars   = (charsHandle)(**inTextHandle).hText;
  4766.         noOfLines = lineOfEnd - lineOfStart + 1;
  4767.         
  4768.         if (whichLine<0)
  4769.             whichLine = noOfLines + whichLine + 1;
  4770.             
  4771.         noOfLines = CountLines(inTextHandle);
  4772.         whichLine = whichLine + lineOfStart - 1; /* convert offset relative to offset absolute */
  4773.         
  4774.         /* End of addition */
  4775.         
  4776.         if (whichLine<=lineOfEnd)
  4777.             {
  4778.                 *lineStartChar = (**inTextHandle).lineStarts[whichLine-1];
  4779.                 if (whichLine==noOfLines)
  4780.                     *lineLength  = (**inTextHandle).teLength;
  4781.                 else
  4782.                     *lineLength  = (**inTextHandle).lineStarts[whichLine];
  4783.                 *lineLength    = *lineLength-*lineStartChar;
  4784.                 /*
  4785.                     Don't return CR
  4786.                 */
  4787.                 if ((**myChars)[ *lineStartChar+*lineLength-1] == 13)
  4788.                     *lineLength = *lineLength-1;
  4789.             }
  4790.         else
  4791.             {
  4792.                 if (whichLine<noOfLines)
  4793.                   *lineStartChar = (**inTextHandle).lineStarts[whichLine]; /* start of whichLine++ */
  4794.                 else
  4795.                     *lineStartChar = (**inTextHandle).teLength;
  4796.                 *lineLength    = 0;
  4797.             }
  4798.     } /* GetLineInfo */
  4799.     
  4800. pascal OSErr TextElemFromWndwAccessor(DescType     wantClass,
  4801.                                                                         const AEDesc *container,
  4802.                                                                         DescType     containerClass,
  4803.                                                                         DescType     form,
  4804.                                                                         const AEDesc *selectionData,
  4805.                                                                         AEDesc       *value,
  4806.                                                                             long         theRefCon)
  4807.   { 
  4808. #pragma unused (theRefCon)
  4809.     
  4810.         OSErr       myErr;
  4811.         OSErr       ignoreErr;
  4812.         WindowToken theWindow;
  4813.         Size        actSize;
  4814.         long        index;
  4815.         TextToken   theTextToken;
  4816.         AERecord    selectionRecord;
  4817.         TextToken   startText;
  4818.         TextToken   stopText;
  4819.         DescType    returnedType;
  4820.         AEDesc      windDesc;
  4821.         TEHandle    theHTE;
  4822.         DPtr        theDocument;
  4823.         short       wordStartChar;
  4824.         short       wordLength;
  4825.     
  4826.         myErr = -1700;    /* or whatever */
  4827.         
  4828.         selectionRecord.dataHandle = nil;
  4829.         
  4830.         /* do some checking for robustness' sake */
  4831.         
  4832.         if ((containerClass != cWindow) ||
  4833.               ((wantClass != cChar) && 
  4834.                  (wantClass != cSpot) && 
  4835.                  (wantClass != cWord) &&
  4836.                  (wantClass != cLine)    ) ||
  4837.               ((form!=formRange) && (form!=formAbsolutePosition)))
  4838.             return(errAEWrongDataType);
  4839.             
  4840.         /* let's get the window which contains the text element */
  4841.         
  4842.         myErr = AECoerceDesc(container, typeMyWndw, &windDesc);
  4843.         GetRawDataFromDescriptor(    &windDesc,
  4844.                                                             (Ptr)&theWindow,
  4845.                                                             sizeof(theWindow),
  4846.                                                             &actSize);
  4847.         myErr = AEDisposeDesc(&windDesc);
  4848.                                                             
  4849.         if (theWindow==nil)
  4850.             myErr = errAEIllegalIndex;
  4851.         else
  4852.             {
  4853.                                                                                     
  4854.                 theTextToken.tokenWindow = theWindow;
  4855.                 
  4856.                 theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  4857.                 theHTE      = theDocument->theText;
  4858.                 
  4859.                 if (form == formAbsolutePosition)
  4860.                     {
  4861.                         myErr = GetLongIntFromDescriptor(selectionData, &index);
  4862.                         
  4863.                         if (wantClass==cSpot)
  4864.                             {
  4865.                               if (index<0)
  4866.                                     theTextToken.tokenOffset = (**theHTE).teLength+index+2; /* Past last char */
  4867.                                 else
  4868.                                     theTextToken.tokenOffset = index;
  4869.                                     
  4870.                                 theTextToken.tokenLength = 0;
  4871.                             }
  4872.                             
  4873.                         if (wantClass==cChar)
  4874.                             {
  4875.                                 if (index<0)
  4876.                                     theTextToken.tokenOffset = (**theHTE).teLength+index+1;
  4877.                                 else
  4878.                                   theTextToken.tokenOffset = index;
  4879.                                     
  4880.                                 theTextToken.tokenLength = 1;
  4881.                             }
  4882.                             
  4883.                         if (wantClass==cWord)
  4884.                             {
  4885.                                 wordStartChar = 0;
  4886.                                 wordLength    = (**theHTE).teLength;
  4887.                                 GetWordInfo(index, theHTE, &wordStartChar, &wordLength); /* zero based */
  4888.                                 theTextToken.tokenOffset = wordStartChar+1;
  4889.                                 theTextToken.tokenLength = wordLength;
  4890.                             }
  4891.                             
  4892.                         if (wantClass==cLine)
  4893.                             {
  4894.                                 wordStartChar = 0;
  4895.                                 wordLength    = (**theHTE).teLength;
  4896.                                 GetLineInfo(index, theHTE, &wordStartChar, &wordLength); /* zero based */
  4897.                                 theTextToken.tokenOffset = wordStartChar+1;
  4898.                                 theTextToken.tokenLength = wordLength;
  4899.                             }
  4900.                     }    /* of formAbsolutePosition */
  4901.                 
  4902.                 if (form == formRange)
  4903.                     {
  4904.                         /* coerce the selection data into an AERecord */
  4905.                         
  4906.                          myErr = AECoerceDesc(selectionData, typeAERecord, &selectionRecord);
  4907.                         
  4908.                         /* get the start object as a text token - 
  4909.                                 this will reenter this proc but as formAbsolutePosition via the coercion handler*/
  4910.                         
  4911.                         myErr =  AEGetKeyPtr(&selectionRecord,
  4912.                                                                  keyAERangeStart,
  4913.                                                                  typeMyText,
  4914.                                                                  &returnedType,
  4915.                                                                  (Ptr)&startText,
  4916.                                                                  sizeof(startText),
  4917.                                                                  &actSize);
  4918.                         
  4919.                         /* now do the same for the stop object */
  4920.                         
  4921.                         if (myErr==noErr)
  4922.                             myErr =  AEGetKeyPtr(&selectionRecord,
  4923.                                                                      keyAERangeStop,
  4924.                                                                      typeMyText,
  4925.                                                                      &returnedType,
  4926.                                                                      (Ptr)&stopText,
  4927.                                                                      sizeof(stopText),
  4928.                                                                      &actSize);
  4929.                         
  4930.                         if (myErr==noErr)
  4931.                             if ((theTextToken.tokenWindow != stopText.tokenWindow) ||
  4932.                                   (theTextToken.tokenWindow != startText.tokenWindow))
  4933.                                 myErr = errAECorruptData;    /* or whatever ????*/
  4934.                             
  4935.                         theTextToken.tokenOffset  = startText.tokenOffset;            
  4936.                         theTextToken.tokenLength  = stopText.tokenOffset +
  4937.                                                                                  stopText.tokenLength - 
  4938.                                                                                  startText.tokenOffset;
  4939.                                                                                  
  4940.                         if (theTextToken.tokenLength<0)
  4941.                             myErr = errAECorruptData;    /* or whatever */
  4942.                                                                              
  4943.                         ignoreErr = AEDisposeDesc(&selectionRecord);
  4944.                         
  4945.                     }    /* of formRange */
  4946.             }
  4947.         
  4948.         /* return theTextToken in a descriptor */
  4949.         
  4950.         if (myErr==noErr)
  4951.             myErr = AECreateDesc(typeMyText,
  4952.                                                      (Ptr)&theTextToken,
  4953.                                                      sizeof(theTextToken),
  4954.                                                      value);
  4955.                 
  4956.         return(myErr);
  4957.     }    /* TextElemFromWndwAccessor */
  4958.  
  4959. pascal OSErr TextElemFromTextAccessor(DescType     wantClass,
  4960.                                                                         const AEDesc *container,
  4961.                                                                         DescType     containerClass,
  4962.                                                                         DescType     form,
  4963.                                                                         const AEDesc *selectionData,
  4964.                                                                         AEDesc       *value,
  4965.                                                                         long         theRefCon)
  4966.     { 
  4967. #pragma unused (theRefCon, containerClass)
  4968.  
  4969.         OSErr       myErr;
  4970.         Size        actSize;
  4971.         long        index;
  4972.         TextToken   theTextToken;
  4973.         AERecord    selectionRecord;
  4974.         TextToken   startText;
  4975.         TextToken   stopText;
  4976.         DescType    returnedType;
  4977.         AEDesc      textDesc;
  4978.         TEHandle    theHTE;
  4979.         short       wordStartChar;
  4980.         short       wordLength;
  4981.         DPtr        theDocument;
  4982.     
  4983.         myErr = -1700;    /* or whatever */
  4984.             
  4985.         /* do some checking for robustness' sake */
  4986.         
  4987.         if (((wantClass != cChar) && 
  4988.                  (wantClass != cSpot) && 
  4989.                  (wantClass != cLine) && 
  4990.                  (wantClass != cWord)) ||
  4991.               ((form != formAbsolutePosition) && (form != formRange)))
  4992.             return(errAEWrongDataType);
  4993.         
  4994.         /* let's get the src text */
  4995.         
  4996.         myErr = AECoerceDesc(container, typeMyText, &textDesc);
  4997.         GetRawDataFromDescriptor(    &textDesc,
  4998.                                                             (Ptr)&theTextToken,
  4999.                                                             sizeof(theTextToken),
  5000.                                                             &actSize);
  5001.                                                             
  5002.         myErr = AEDisposeDesc(&textDesc);
  5003.                                                             
  5004.         theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  5005.         theHTE      = theDocument->theText;
  5006.         
  5007.         if (form == formAbsolutePosition)
  5008.             {
  5009.                 myErr = GetLongIntFromDescriptor(selectionData, &index);
  5010.                 
  5011.                 if (wantClass==cSpot)
  5012.                     {
  5013.                         if (index<0)
  5014.                             theTextToken.tokenOffset = theTextToken.tokenOffset+index+1+theTextToken.tokenLength;
  5015.                         else
  5016.                             theTextToken.tokenOffset = theTextToken.tokenOffset+index-1;
  5017.                         theTextToken.tokenLength = 0;
  5018.                     }
  5019.                     
  5020.                 if (wantClass==cChar)
  5021.                     {
  5022.                         if (index<0)
  5023.                             theTextToken.tokenOffset = theTextToken.tokenOffset+index+1+theTextToken.tokenLength;
  5024.                         else
  5025.                             theTextToken.tokenOffset = theTextToken.tokenOffset+index-1;
  5026.                         theTextToken.tokenLength = 1;
  5027.                     }
  5028.                                     
  5029.                 if (wantClass==cWord)
  5030.                     {
  5031.                         wordStartChar = theTextToken.tokenOffset-1;
  5032.                         wordLength    = theTextToken.tokenLength;
  5033.                         
  5034.                         GetWordInfo(index, theHTE, &wordStartChar, &wordLength);/*zero based*/
  5035.                         
  5036.                         theTextToken.tokenOffset = wordStartChar+1;
  5037.                         theTextToken.tokenLength = wordLength;
  5038.                     }
  5039.                     
  5040.                 if (wantClass==cLine)
  5041.                     {
  5042.                         wordStartChar = theTextToken.tokenOffset-1;
  5043.                         wordLength    = theTextToken.tokenLength;
  5044.                         
  5045.                         GetLineInfo(index, theHTE, &wordStartChar, &wordLength);
  5046.                         
  5047.                         theTextToken.tokenOffset = wordStartChar+1;
  5048.                         theTextToken.tokenLength = wordLength;
  5049.                     }
  5050.     
  5051.             }    /* of formAbsolutePosition */
  5052.         
  5053.         
  5054.         if (form == formRange)
  5055.             {
  5056.                 /* coerce the selection data into an AERecord */
  5057.                 
  5058.                  myErr = AECoerceDesc(selectionData, typeAERecord, &selectionRecord);
  5059.                 
  5060.                 /* get the start object as a text token - 
  5061.                         this will reenter this proc but as formAbsolutePosition via the coercion handler*/
  5062.                 
  5063.                 myErr =  AEGetKeyPtr(&selectionRecord,
  5064.                                                          keyAERangeStart,
  5065.                                                          typeMyText,
  5066.                                                          &returnedType,
  5067.                                                          (Ptr)&startText,
  5068.                                                          sizeof(startText),
  5069.                                                          &actSize);
  5070.                 
  5071.                 /* now do the same for the stop object */
  5072.                 
  5073.                 if (myErr==noErr)
  5074.                     myErr =  AEGetKeyPtr(&selectionRecord,
  5075.                                                              keyAERangeStop,
  5076.                                                              typeMyText,
  5077.                                                              &returnedType,
  5078.                                                              (Ptr)&stopText,
  5079.                                                              sizeof(stopText),
  5080.                                                              &actSize);
  5081.                 
  5082.                 if (myErr==noErr)
  5083.                     if ((theTextToken.tokenWindow != stopText.tokenWindow) ||
  5084.                           (theTextToken.tokenWindow != startText.tokenWindow))
  5085.                         myErr = errAECorruptData;    /* or whatever */
  5086.                     
  5087.                 theTextToken.tokenOffset  = startText.tokenOffset;            
  5088.                 theTextToken.tokenLength  = stopText.tokenOffset +
  5089.                                                                          stopText.tokenLength - 
  5090.                                                                          startText.tokenOffset;
  5091.                                                                      
  5092.                 myErr = AEDisposeDesc(&selectionRecord);
  5093.             }    /* of formRange */
  5094.             
  5095.         /* return theTextToken in a descriptor */
  5096.         
  5097.         myErr = AECreateDesc( typeMyText,
  5098.                                                     (Ptr)&theTextToken,
  5099.                                                     sizeof(theTextToken),
  5100.                                                     value);
  5101.             
  5102.         return(myErr);
  5103.     }    /* TextElemFromTextAccessor */
  5104.  
  5105. pascal OSErr PropertyFromTextAccessor(DescType     wantClass,
  5106.                                                                             const AEDesc *container,
  5107.                                                                             DescType     containerClass,
  5108.                                                                             DescType     form, 
  5109.                                                                             const AEDesc *selectionData,
  5110.                                                                             AEDesc       *value,
  5111.                                                                             long         theRefCon)
  5112.   {
  5113. #pragma unused (theRefCon, containerClass)
  5114.     
  5115.         OSErr         myErr;
  5116.         OSErr         ignoreErr;
  5117.         TextToken     theTextToken;
  5118.         DescType      theProperty;
  5119.         AEDesc        textDesc;
  5120.         AEDesc        propDesc;
  5121.         Size          actualSize;
  5122.         textPropToken myTextProp;
  5123.             
  5124.         value->dataHandle   = nil;
  5125.         textDesc.dataHandle = nil;
  5126.         propDesc.dataHandle = nil;
  5127.         
  5128.         if ((wantClass != cProperty) ||
  5129.                 (form != formPropertyID))
  5130.             {
  5131.                 return(errAEWrongDataType);
  5132.             }
  5133.         
  5134.         /* get the text token */
  5135.         myErr = AECoerceDesc(container, typeMyText, &textDesc);
  5136.         GetRawDataFromDescriptor(    &textDesc,
  5137.                                                             (Ptr)&theTextToken,
  5138.                                                             sizeof(theTextToken),
  5139.                                                             &actualSize);
  5140.                 
  5141.         /* get the property */
  5142.         myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5143.         GetRawDataFromDescriptor(&propDesc,
  5144.                                                          (Ptr)&theProperty,
  5145.                                                          sizeof(theProperty),
  5146.                                                          &actualSize);
  5147.         /*
  5148.             Combine the two into single token
  5149.         */
  5150.         myTextProp.propertyTextToken = theTextToken;
  5151.         myTextProp.propertyProperty  = theProperty;
  5152.         
  5153.         myErr = AECreateDesc(typeMyTextProp,
  5154.                                                  (Ptr)&myTextProp,
  5155.                                                  sizeof(myTextProp),
  5156.                                                  value);
  5157.             
  5158.         if (textDesc.dataHandle)
  5159.             ignoreErr = AEDisposeDesc(&textDesc);
  5160.             
  5161.         if (propDesc.dataHandle)
  5162.             ignoreErr = AEDisposeDesc(&propDesc);
  5163.             
  5164.         return(myErr);
  5165. }    /* PropertyFromTextAccessor */
  5166.  
  5167. pascal OSErr PropertyFromWndwAccessor(DescType     wantClass,
  5168.                                                                         const AEDesc *container,
  5169.                                                                         DescType     containerClass,
  5170.                                                                         DescType     form, 
  5171.                                                                         const AEDesc *selectionData,
  5172.                                                                         AEDesc       *value,
  5173.                                                                         long         theRefCon)
  5174.   { 
  5175. #pragma unused (theRefCon, containerClass)
  5176.     
  5177.         OSErr           myErr;
  5178.         OSErr           ignoreErr;
  5179.         WindowToken     theWindowToken;
  5180.         DescType        theProperty;
  5181.         AEDesc          windowDesc;
  5182.         AEDesc          propDesc;
  5183.         Size            actualSize;
  5184.         windowPropToken myWindowProp;
  5185.         
  5186.         value->dataHandle     = nil;
  5187.         windowDesc.dataHandle = nil;
  5188.         propDesc.dataHandle   = nil;
  5189.         
  5190.         if ((wantClass != cProperty) ||
  5191.               (form != formPropertyID))
  5192.             {
  5193.                 return(errAEWrongDataType);
  5194.             }
  5195.         
  5196.         /* get the window token - it's the container */
  5197.         
  5198.         myErr = AECoerceDesc(container, typeMyWndw, &windowDesc);
  5199.         GetRawDataFromDescriptor(    &windowDesc,
  5200.                                                             (Ptr)&theWindowToken,
  5201.                                                             sizeof(theWindowToken),
  5202.                                                             &actualSize);
  5203.                                                             
  5204.         /* Check the window exists */
  5205.         
  5206.         if (theWindowToken==nil)
  5207.             myErr = errAEIllegalIndex;
  5208.         else
  5209.             {
  5210.             
  5211.                 /* get the property - it's in the selection data */
  5212.                 
  5213.                 myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5214.                 GetRawDataFromDescriptor(    &propDesc,
  5215.                                                                     (Ptr)&theProperty,
  5216.                                                                     sizeof(theProperty),
  5217.                                                                     &actualSize);
  5218.                 /*
  5219.                     Combine the two into single token
  5220.                 */
  5221.                 myWindowProp.tokenWindowToken = theWindowToken;
  5222.                 myWindowProp.tokenProperty    = theProperty;
  5223.                 
  5224.                 myErr = AECreateDesc( typeMyWindowProp,
  5225.                                                             (Ptr)&myWindowProp,
  5226.                                                             sizeof(myWindowProp),
  5227.                                                             value);
  5228.             }
  5229.             
  5230.         if (windowDesc.dataHandle)
  5231.             ignoreErr = AEDisposeDesc(&windowDesc);
  5232.             
  5233.         if (propDesc.dataHandle)
  5234.             ignoreErr = AEDisposeDesc(&propDesc);
  5235.             
  5236.         return(myErr);
  5237.   }    /* PropertyFromWndwAccessor */
  5238.  
  5239. pascal OSErr PropertyFromApplAccessor(DescType     wantClass,
  5240.                                                                             const AEDesc *container,
  5241.                                                                             DescType     containerClass,
  5242.                                                                             DescType     form, 
  5243.                                                                             const AEDesc *selectionData,
  5244.                                                                             AEDesc       *value,
  5245.                                                                             long         theRefCon)
  5246.   {
  5247. #pragma unused (theRefCon, containerClass)
  5248.     
  5249.         OSErr         myErr;
  5250.         OSErr         ignoreErr;
  5251.         appToken      theApplToken;
  5252.         DescType      theProperty;
  5253.         AEDesc        applDesc;
  5254.         AEDesc        propDesc;
  5255.         Size          actualSize;
  5256.         applPropToken myApplProp;
  5257.             
  5258.         value->dataHandle     = nil;
  5259.         applDesc.dataHandle   = nil;
  5260.         propDesc.dataHandle   = nil;
  5261.         
  5262.         if ((wantClass != cProperty) ||
  5263.               (form != formPropertyID))
  5264.             {
  5265.                 return(errAEWrongDataType);
  5266.             }
  5267.         
  5268.         /* get the application token - it's the container */
  5269.         
  5270.         myErr = AECoerceDesc(container, typeMyAppl, &applDesc);
  5271.         GetRawDataFromDescriptor(&applDesc,
  5272.                                                          (Ptr)&theApplToken,
  5273.                                                          sizeof(theApplToken),
  5274.                                                          &actualSize);
  5275.                 
  5276.         /* get the property - it's in the selection data */
  5277.         
  5278.         myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5279.         GetRawDataFromDescriptor(&propDesc,
  5280.                                                          (Ptr)&theProperty,
  5281.                                                          sizeof(theProperty),
  5282.                                                          &actualSize);
  5283.         /*
  5284.             Combine the two into single token
  5285.         */
  5286.         myApplProp.tokenApplToken    = theApplToken;
  5287.         myApplProp.tokenApplProperty = theProperty;
  5288.         
  5289.         myErr = AECreateDesc(typeMyApplProp,
  5290.                                                  (Ptr)&myApplProp,
  5291.                                                  sizeof(myApplProp),
  5292.                                                  value);
  5293.             
  5294.         if (applDesc.dataHandle)
  5295.             ignoreErr = AEDisposeDesc(&applDesc);
  5296.             
  5297.         if (propDesc.dataHandle)
  5298.             ignoreErr = AEDisposeDesc(&propDesc);
  5299.             
  5300.         return(myErr);
  5301. }    /* PropertyFromApplAccessor */
  5302.  
  5303. pascal OSErr MenuNameToMenuToken(const Str255 theName, MenuToken *theToken)
  5304.     {
  5305.         short   index;
  5306.         
  5307.         for (index=appleM; index<=kLastMenu; index++)
  5308.          {
  5309.                 if (IUEqualString(theName, (**(myMenus[index])).menuData)==0)
  5310.                   {
  5311.                         theToken->theTokenMenu = myMenus[index];
  5312.                         theToken->theTokenID   = index+appleID;
  5313.                         return(noErr);
  5314.                     }
  5315.          }
  5316.         return(errAEIllegalIndex);
  5317.     }
  5318.  
  5319. pascal OSErr MenuFromNullAccessor(DescType      wantClass,
  5320.                                                                     const AEDesc  *container,
  5321.                                                                     DescType      containerClass,
  5322.                                                                     DescType      form, 
  5323.                                                                     const AEDesc  *selectionData,
  5324.                                                                     AEDesc        *value,
  5325.                                                                     long          theRefCon)
  5326.     {
  5327. #pragma unused (container,theRefCon)
  5328.     
  5329.         OSErr       myErr;
  5330.         Str255      nameStr;
  5331.         MenuToken   theMenu;
  5332.         short       index;
  5333.         AEDesc      resultDesc;
  5334.         
  5335.         myErr = errAEBadKeyForm;    /* or whatever */
  5336.         
  5337.         value->dataHandle     = nil;
  5338.         resultDesc.dataHandle = nil;
  5339.         
  5340.         /* 
  5341.             should only be called with wantClass = cMenu and
  5342.             with containerClass = typeNull or typeMyAppl.
  5343.             Currently accept as either formName or formAbsolutePosition
  5344.         */
  5345.         
  5346.         if ((wantClass != cMenu) || 
  5347.               ((containerClass != typeNull) && (containerClass != typeMyAppl)) ||
  5348.                !((form == formName) || (form == formAbsolutePosition)))
  5349.             return(errAEWrongDataType);
  5350.         
  5351.         if (form == formName)
  5352.             {
  5353.                 myErr = GetPStringFromDescriptor(selectionData, (char *)nameStr);
  5354.                 myErr = MenuNameToMenuToken(nameStr, &theMenu);
  5355.             }
  5356.         
  5357.         if (form == formAbsolutePosition)
  5358.             {
  5359.                 myErr     = GetIntegerFromDescriptor(selectionData, &index);
  5360.                 if (index<0)
  5361.                     index = kLastMenu + index + 1;
  5362.                     
  5363.                 if (index>0 && index<=kLastMenu+1)
  5364.                     {
  5365.                         theMenu.theTokenMenu = myMenus[index-1];
  5366.                         theMenu.theTokenID   = index-1+appleID;
  5367.                     }
  5368.                 else
  5369.               myErr = errAEIllegalIndex;    /* or whatever */
  5370.             }
  5371.             
  5372.         if (myErr == noErr)
  5373.             myErr = AECreateDesc(typeMyMenu, (Ptr)&theMenu, sizeof(theMenu), value);
  5374.                 
  5375.         return(myErr);
  5376.     }    /* MenuFromNullAccessor */
  5377.  
  5378. pascal OSErr PropertyFromMenuAccessor(DescType     wantClass,
  5379.                                                                             const AEDesc *container,
  5380.                                                                             DescType     containerClass,
  5381.                                                                             DescType     form, 
  5382.                                                                             const AEDesc *selectionData,
  5383.                                                                             AEDesc       *value,
  5384.                                                                             long         theRefCon)
  5385.   {
  5386. #pragma unused (theRefCon, containerClass)
  5387.     
  5388.         OSErr         myErr;
  5389.         OSErr         ignoreErr;
  5390.         MenuToken     theMenuToken;
  5391.         DescType      theProperty;
  5392.         AEDesc        menuDesc;
  5393.         AEDesc        propDesc;
  5394.         Size          actualSize;
  5395.         MenuPropToken myMenuProp;
  5396.             
  5397.         value->dataHandle     = nil;
  5398.         menuDesc.dataHandle   = nil;
  5399.         propDesc.dataHandle   = nil;
  5400.         
  5401.         if ((wantClass != cProperty) ||
  5402.               (form != formPropertyID))
  5403.             {
  5404.                 return(errAEWrongDataType);
  5405.             }
  5406.         
  5407.         /* get the menu token - it's the container */
  5408.         
  5409.         myErr = AECoerceDesc(container, typeMyMenu, &menuDesc);
  5410.         GetRawDataFromDescriptor(&menuDesc,
  5411.                                                          (Ptr)&theMenuToken,
  5412.                                                          sizeof(theMenuToken),
  5413.                                                          &actualSize);
  5414.                 
  5415.         /* get the property - it's in the selection data */
  5416.         
  5417.         myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5418.         GetRawDataFromDescriptor(&propDesc,
  5419.                                                          (Ptr)&theProperty,
  5420.                                                          sizeof(theProperty),
  5421.                                                          &actualSize);
  5422.         /*
  5423.             Combine the two into single token
  5424.         */
  5425.         myMenuProp.theMenuToken = theMenuToken;
  5426.         myMenuProp.theMenuProp  = theProperty;
  5427.         
  5428.         myErr = AECreateDesc(typeMyMenuProp,
  5429.                                                  (Ptr)&myMenuProp,
  5430.                                                  sizeof(myMenuProp),
  5431.                                                  value);
  5432.             
  5433.         if (menuDesc.dataHandle)
  5434.             ignoreErr = AEDisposeDesc(&menuDesc);
  5435.             
  5436.         if (propDesc.dataHandle)
  5437.             ignoreErr = AEDisposeDesc(&propDesc);
  5438.             
  5439.         return(myErr);
  5440. }    /* PropertyFromMenuAccessor */
  5441.  
  5442. pascal OSErr PropertyFromMenuItemAccessor(DescType     wantClass,
  5443.                                                                                     const AEDesc *container,
  5444.                                                                                     DescType     containerClass,
  5445.                                                                                     DescType     form, 
  5446.                                                                                     const AEDesc *selectionData,
  5447.                                                                                     AEDesc       *value,
  5448.                                                                                     long         theRefCon)
  5449.   {
  5450. #pragma unused (theRefCon, containerClass)
  5451.     
  5452.         OSErr         myErr;
  5453.         OSErr         ignoreErr;
  5454.         MenuItemToken theMenuItemToken;
  5455.         DescType      theProperty;
  5456.         AEDesc        itemDesc;
  5457.         AEDesc        propDesc;
  5458.         Size          actualSize;
  5459.         MenuItemPropToken myItemProp;
  5460.             
  5461.         value->dataHandle     = nil;
  5462.         itemDesc.dataHandle   = nil;
  5463.         propDesc.dataHandle   = nil;
  5464.         
  5465.         if ((wantClass != cProperty) ||
  5466.               (form != formPropertyID))
  5467.             {
  5468.                 return(errAEWrongDataType);
  5469.             }
  5470.         
  5471.         /* get the menu token - it's the container */
  5472.         
  5473.         myErr = AECoerceDesc(container, typeMyMenuItem, &itemDesc);
  5474.         GetRawDataFromDescriptor(&itemDesc,
  5475.                                                          (Ptr)&theMenuItemToken,
  5476.                                                          sizeof(theMenuItemToken),
  5477.                                                          &actualSize);
  5478.                 
  5479.         /* get the property - it's in the selection data */
  5480.         
  5481.         myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  5482.         GetRawDataFromDescriptor(&propDesc,
  5483.                                                          (Ptr)&theProperty,
  5484.                                                          sizeof(theProperty),
  5485.                                                          &actualSize);
  5486.         /*
  5487.             Combine the two into single token
  5488.         */
  5489.         myItemProp.theItemToken  = theMenuItemToken;
  5490.         myItemProp.theItemProp   = theProperty;
  5491.         
  5492.         myErr = AECreateDesc(typeMyItemProp,
  5493.                                                  (Ptr)&myItemProp,
  5494.                                                  sizeof(myItemProp),
  5495.                                                  value);
  5496.             
  5497.         if (itemDesc.dataHandle)
  5498.             ignoreErr = AEDisposeDesc(&itemDesc);
  5499.             
  5500.         if (propDesc.dataHandle)
  5501.             ignoreErr = AEDisposeDesc(&propDesc);
  5502.             
  5503.         return(myErr);
  5504. }    /* PropertyFromMenuItemAccessor */
  5505.  
  5506. pascal OSErr ItemNameToItemIndex(const Str255 theName, MenuHandle theMenu, short *theIndex)
  5507.     {
  5508.         short   index;
  5509.         short   maxItems;
  5510.         Str255  menuName;
  5511.         
  5512.         maxItems = CountMItems(theMenu);
  5513.         
  5514.         for (index=1; index<=maxItems; index++)
  5515.             {
  5516.                 GetItem(theMenu, index, menuName);
  5517.                 if (IUEqualString(theName, menuName)==0)
  5518.                     {
  5519.                         *theIndex = index;
  5520.                         return(noErr);
  5521.                     }
  5522.             }
  5523.         return(errAEIllegalIndex);
  5524.     }
  5525.     
  5526. pascal OSErr MenuItemFromMenuAccessor(DescType     wantClass,
  5527.                                                                             const AEDesc *container,
  5528.                                                                             DescType     containerClass,
  5529.                                                                             DescType     form, 
  5530.                                                                             const AEDesc *selectionData,
  5531.                                                                             AEDesc       *value,
  5532.                                                                             long         theRefCon)
  5533.   {
  5534. #pragma unused (theRefCon)
  5535.     
  5536.         OSErr         myErr;
  5537.         OSErr         ignoreErr;
  5538.         MenuItemToken theMenuItemToken;
  5539.         MenuToken     theMenuToken;
  5540.         AEDesc        menuDesc;
  5541.         Size          actualSize;
  5542.         Str255        nameStr;
  5543.         short         maxItems;
  5544.         short         index;
  5545.             
  5546.         value->dataHandle     = nil;
  5547.         menuDesc.dataHandle   = nil;
  5548.         
  5549.         if ((wantClass != cMenuItem) || (containerClass != cMenu) ||
  5550.               ((form != formAbsolutePosition) && (form != formName)))
  5551.             {
  5552.                 return(errAEWrongDataType);
  5553.             }
  5554.         
  5555.         /* get the menu token - it's the container */
  5556.         
  5557.         myErr = AECoerceDesc(container, typeMyMenu, &menuDesc);
  5558.         GetRawDataFromDescriptor(&menuDesc,
  5559.                                                          (Ptr)&theMenuToken,
  5560.                                                          sizeof(theMenuToken),
  5561.                                                          &actualSize);
  5562.                 
  5563.         if (form==formAbsolutePosition)
  5564.             {
  5565.                 myErr = GetIntegerFromDescriptor(selectionData, &index);
  5566.                 maxItems = CountMItems(theMenuToken.theTokenMenu);
  5567.                 
  5568.                 if (index<0)
  5569.                     index = maxItems + index + 1;
  5570.                     
  5571.                 if ((index<1) || (index>maxItems))
  5572.                   myErr = errAEIllegalIndex;
  5573.             }
  5574.             
  5575.         if (form == formName)
  5576.             {
  5577.                 myErr  = GetPStringFromDescriptor(selectionData, (char *)nameStr);
  5578.                 myErr  = ItemNameToItemIndex(nameStr, theMenuToken.theTokenMenu, &index);
  5579.             }
  5580.         
  5581.         /*
  5582.             Combine the two into single token
  5583.         */
  5584.     
  5585.         theMenuItemToken.theMenuToken  = theMenuToken;
  5586.         theMenuItemToken.theTokenItem  = index;
  5587.         
  5588.         if (myErr==noErr)
  5589.             myErr = AECreateDesc(typeMyMenuItem,
  5590.                                                      (Ptr)&theMenuItemToken,
  5591.                                                      sizeof(theMenuItemToken),
  5592.                                                      value);
  5593.             
  5594.         if (menuDesc.dataHandle)
  5595.             ignoreErr = AEDisposeDesc(&menuDesc);
  5596.             
  5597.         return(myErr);
  5598. }    /* MenuItemFromMenuAccessor */
  5599.  
  5600. /*******************************************************************************/
  5601. /*
  5602.     Stuff for counting objects
  5603. */
  5604.  
  5605. pascal OSErr MyCountProc(DescType     desiredType,
  5606.                                                DescType     containerClass,
  5607.                                                const AEDesc *container,
  5608.                                                long         *result)
  5609.                                             
  5610. /* so far all I count is:
  5611.   (1) the number of active windows in the app;
  5612.   (2) the number of words in a window 
  5613. */
  5614.  
  5615.     {   
  5616.         OSErr       myErr;
  5617.         WindowToken theWindowToken;
  5618.         DPtr        theDocument;
  5619.         TEHandle    theHTE;
  5620.         AEDesc      newDesc;
  5621.         short       wordStart;
  5622.         short       wordLength;
  5623.         Size        tokenSize;
  5624.         TextToken   theTextToken;
  5625.             
  5626.         *result = -1;    /* easily recognized illegal value */
  5627.             
  5628.         myErr = errAEWrongDataType;
  5629.         
  5630.         if (desiredType == cWindow)
  5631.             {
  5632.                 if ((containerClass == typeNull) ||
  5633.                       (containerClass == cApplication))
  5634.                     *result = CountWindows();
  5635.             }
  5636.             
  5637.         if ((desiredType == cWord) ||
  5638.               (desiredType == cLine) ||
  5639.               (desiredType == cChar))
  5640.             {
  5641.                 myErr = AECoerceDesc(container, typeMyWndw, &newDesc);
  5642.                 if (newDesc.descriptorType!=typeNull)
  5643.                     {
  5644.                         GetRawDataFromDescriptor(&newDesc,
  5645.                                                                          (Ptr)&theWindowToken,
  5646.                                                                          sizeof(theWindowToken),
  5647.                                                                          &tokenSize);
  5648.                                                                          
  5649.                         myErr = AEDisposeDesc(&newDesc);
  5650.                                                                          
  5651.                         if (theWindowToken==nil)
  5652.                             myErr = errAEIllegalIndex;
  5653.                         else
  5654.                             {
  5655.                         
  5656.                                 theDocument = DPtrFromWindowPtr(theWindowToken);
  5657.                                 theHTE      = theDocument->theText;
  5658.                                 
  5659.                                 if (desiredType == cWord)
  5660.                                     {
  5661.                                         wordStart   = 0;
  5662.                                         wordLength  = (**theHTE).teLength;
  5663.                                         *result     = CountWords(theHTE, wordStart, wordLength);
  5664.                                     }
  5665.                                 
  5666.                                 if (desiredType == cChar)
  5667.                                     *result = (**theHTE).teLength;
  5668.                                     
  5669.                                 if (desiredType == cLine)
  5670.                                     *result = CountLines(theHTE);
  5671.                                     
  5672.                             }
  5673.                     }
  5674.                     
  5675.                 myErr = AECoerceDesc(container, typeMyText, &newDesc);
  5676.                 if (newDesc.descriptorType!=typeNull)
  5677.                     {
  5678.                         GetRawDataFromDescriptor(    &newDesc,
  5679.                                                                             (Ptr)&theTextToken,
  5680.                                                                             sizeof(theTextToken),
  5681.                                                                             &tokenSize);
  5682.                                                                          
  5683.                         myErr = AEDisposeDesc(&newDesc);
  5684.                                                                          
  5685.                         theDocument = DPtrFromWindowPtr(theTextToken.tokenWindow);
  5686.                         theHTE      = theDocument->theText;
  5687.                         
  5688.                         if (desiredType == cWord)
  5689.                             {
  5690.                                 wordStart   = theTextToken.tokenOffset-1;
  5691.                                 wordLength  = theTextToken.tokenLength;
  5692.                                 *result     = CountWords(theHTE, wordStart, wordLength);
  5693.                             }
  5694.                         
  5695.                         if (desiredType == cChar)
  5696.                             *result = theTextToken.tokenLength;
  5697.                             
  5698.                         if (desiredType == cLine)
  5699.                             *result= LineOfOffset(theHTE,theTextToken.tokenOffset-1) - 
  5700.                                              LineOfOffset(theHTE,theTextToken.tokenOffset+theTextToken.tokenLength-1)
  5701.                                              +1;                        
  5702.                     }
  5703.                     
  5704.             }
  5705.             
  5706.         return(myErr);
  5707.     }    /* MyCountProc */
  5708.  
  5709. /*******************************************************************************/
  5710. /*
  5711.     Coercion Handlers - Allow AEResolve to do the hard work
  5712. */
  5713. pascal OSErr CoerceObjToAnything(const AEDesc *theAEDesc,
  5714.                                                              DescType     toType,
  5715.                                                          long         handlerRefCon,
  5716.                                                              AEDesc       *result)
  5717. /*
  5718.     CoerceObjToAnything functions by using AEResolve to do the hard
  5719.     work.
  5720. */
  5721.     {
  5722. #pragma unused (handlerRefCon)
  5723.     
  5724.         OSErr  myErr;
  5725.         AEDesc objDesc;
  5726.             
  5727.         myErr = errAECoercionFail;
  5728.         
  5729.         result->dataHandle = nil;
  5730.         objDesc.dataHandle = nil;
  5731.         
  5732.         
  5733.         if (theAEDesc->descriptorType != typeObjectSpecifier)
  5734.             {
  5735.                 return(errAEWrongDataType);
  5736.             }
  5737.         
  5738.         /* resolve the object specifier */
  5739.         myErr = AEResolve(theAEDesc, kAEIDoMinimum, &objDesc);
  5740.             
  5741.         /* hopefully it's the right type by now, but we'll give it a nudge */
  5742.         if (myErr==noErr)
  5743.             {
  5744.                 myErr = AECoerceDesc(&objDesc, toType, result);
  5745.                 myErr = AEDisposeDesc(&objDesc);
  5746.             }
  5747.             
  5748.         if (result->descriptorType!=toType)
  5749.             {
  5750.                 /*DebugStr('COTA - Not of requested type');*/
  5751.             }
  5752.             
  5753.         return(myErr);
  5754.     }    /* CoerceObjToAnything */
  5755.  
  5756. /*******************************************************************************/
  5757.  
  5758. /*----------------------------------------------------------------------------------------------*/
  5759.  
  5760. /*now for the edition manager event handling code*/
  5761.  
  5762. pascal OSErr GetHandleFromEvent(const AppleEvent *theAppleEvent, SectionHandle *sectionH)
  5763.     
  5764.     {
  5765.         DescType ignoreType;
  5766.         Size          ignoreSize;
  5767.     
  5768.         return(AEGetKeyPtr(theAppleEvent, keyDirectObject, typeSectionH,
  5769.                                              &ignoreType, (Ptr)sectionH, sizeof(SectionHandle), &ignoreSize));
  5770.     } /* GetHandleFromEvent */
  5771.  
  5772. /*----------------------------------------------------------------------------------------------*/
  5773. pascal OSErr DoReadSection(const AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  5774.     
  5775.     {
  5776. #pragma unused (reply, refCon)
  5777.     
  5778.         OSErr         err;
  5779.         SectionHandle sectionH;
  5780.     
  5781.         err = GetHandleFromEvent(theAppleEvent, §ionH);
  5782.         if (IsRegisteredSection(sectionH)==noErr)
  5783.             ReadAnEdition(sectionH);
  5784.         return(err);
  5785.     } /* DoReadSection */
  5786.     
  5787. /*----------------------------------------------------------------------------------------------*/
  5788. pascal OSErr DoWriteSection(const AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
  5789.     
  5790.     {
  5791. #pragma unused (reply, refCon)
  5792.  
  5793.         OSErr         err;
  5794.         SectionHandle sectionH;
  5795.     
  5796.         err = GetHandleFromEvent(theAppleEvent, §ionH);
  5797.         if (IsRegisteredSection(sectionH) == noErr)
  5798.             WriteAnEdition(sectionH);
  5799.         
  5800.         return(err);
  5801.     } /* DoWriteSection */
  5802.  
  5803. /*----------------------------------------------------------------------------------------------*/
  5804. pascal OSErr DoScrollSection(const AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
  5805.     {
  5806. #pragma unused (reply, refCon)
  5807.     
  5808.         OSErr         err;
  5809.         SectionHandle sectionH;
  5810.         SectHandle    aSectHandle;
  5811.     
  5812.         err = GetHandleFromEvent(theAppleEvent, §ionH);
  5813.         /*get at the sectHandle*/
  5814.         aSectHandle = (SectHandle)GetERefCon(sectionH);
  5815.         TESetSelect((**aSectHandle).fStart, (**aSectHandle).fEnd, ((**aSectHandle).fDocument)->theText);
  5816.         ShowSelect((**aSectHandle).fDocument);
  5817.         return(err);
  5818.     }
  5819.  
  5820. /*----------------------------------------------------------------------------------------------*/
  5821. pascal OSErr DoCancelSection(const AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
  5822.  
  5823.     {
  5824. #pragma unused (reply, refCon)
  5825.     
  5826.         OSErr         err;
  5827.         SectionHandle sectionH;
  5828.         SectHandle    aSectHandle;
  5829.             
  5830.         err = GetHandleFromEvent(theAppleEvent, §ionH);
  5831.         aSectHandle = (SectHandle)GetERefCon(sectionH);
  5832.         err = UnRegisterSection(sectionH);
  5833.         DeleteASection(aSectHandle, (**aSectHandle).fDocument);
  5834.         return(noErr);
  5835.     } /* DoCancelSection */
  5836.  
  5837. /* -----------------------------------------------------------------------
  5838.         Name:             InitAppleEvents
  5839.         Purpose:        Initialise the AppleEvent despatch table
  5840.      -----------------------------------------------------------------------**/
  5841.  
  5842. #pragma segment Main
  5843.  
  5844. #define noRefCon -1
  5845.  
  5846. pascal void InitAppleEvents(void)
  5847.     {
  5848.          OSErr aevtErr;
  5849.         
  5850.     gBigBrother = 0;
  5851.         gCharsInBuffer = 0;
  5852.         gTypingBuffer  = (char *)NewPtr(32000);
  5853.         gTypingTargetObject.dataHandle = 0;
  5854.         
  5855.         /*set up the despatch table for the four standard apple events*/
  5856.         
  5857.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(DoOpenApp), noRefCon, false) ;
  5858.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,   NewAEEventHandlerProc(DoOpenDocument), noRefCon, false) ;
  5859.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,  NewAEEventHandlerProc(DoPrintDocuments), noRefCon, false) ;
  5860.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(MyQuit), noRefCon, false) ;
  5861.         
  5862.         /* set up the despatch table for the core AppleEvents for text */
  5863.         
  5864.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEDelete, NewAEEventHandlerProc(DoDeleteEdit),noRefCon, false);
  5865.  
  5866.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECut,    NewAEEventHandlerProc(DoCutEdit),   noRefCon, false);
  5867.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECopy,   NewAEEventHandlerProc(DoCopyEdit),  noRefCon, false);
  5868.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEPaste,  NewAEEventHandlerProc(DoPasteEdit), noRefCon, false);
  5869.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAESetData,NewAEEventHandlerProc(DoSetData),   noRefCon, false);
  5870.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEGetData,NewAEEventHandlerProc(DoGetData),   noRefCon, false);
  5871.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEGetDataSize,NewAEEventHandlerProc(DoGetDataSize),   noRefCon, false);
  5872.         
  5873.         aevtErr = AEInstallEventHandler( kAECoreSuite, kAECountElements,   NewAEEventHandlerProc(HandleNumberOfElements),   noRefCon, false);
  5874.         aevtErr = AEInstallEventHandler( kAECoreSuite, kAECreateElement,   NewAEEventHandlerProc(DoNewElement),   noRefCon, false);
  5875.         aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist,  NewAEEventHandlerProc(DoIsThereA),   noRefCon, false);
  5876.     
  5877.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAEClose,  NewAEEventHandlerProc(DoCloseWindow),noRefCon, false);
  5878.         aevtErr = AEInstallEventHandler( kAECoreSuite,     kAESave,   NewAEEventHandlerProc(DoSaveWindow),noRefCon, false);
  5879.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAERevert, NewAEEventHandlerProc(DoRevertWindow),noRefCon, false);
  5880.         
  5881.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECreatePublisher,    NewAEEventHandlerProc(HandleCreatePub), noRefCon, false);
  5882.         aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEMakeObjectsVisible, NewAEEventHandlerProc(HandleShowSelection), noRefCon, false);
  5883.         
  5884.         /* Now look for recording notifications */
  5885.         
  5886.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEStartedRecording, NewAEEventHandlerProc(HandleStartRecording), noRefCon, false);
  5887.         aevtErr = AEInstallEventHandler( kCoreEventClass, kAEStoppedRecording, NewAEEventHandlerProc(HandleStopRecording), noRefCon, false);
  5888.         
  5889.         /* Now Put in the required object accessors */
  5890.         
  5891.         aevtErr = AESetObjectCallbacks(nil,NewOSLCountProc(MyCountProc),nil,nil,nil,nil,nil);
  5892.         
  5893.         aevtErr = AEInstallObjectAccessor(cWindow,      typeNull,   NewOSLAccessorProc(WindowFromNullAccessor),  0,false);
  5894.         aevtErr = AEInstallObjectAccessor(cWindow,      typeMyAppl, NewOSLAccessorProc(WindowFromNullAccessor),  0,false);
  5895.                 
  5896.         aevtErr = AEInstallObjectAccessor(cApplication, typeNull,   NewOSLAccessorProc(ApplicationFromNullAccessor),  0,false);
  5897.         aevtErr = AEInstallObjectAccessor(cProperty,    typeMyAppl, NewOSLAccessorProc(PropertyFromApplAccessor),0,false);
  5898.         
  5899.         aevtErr = AEInstallObjectAccessor(cChar,             typeMyWndw,NewOSLAccessorProc(TextElemFromWndwAccessor),0,false);
  5900.         aevtErr = AEInstallObjectAccessor(cSpot,             typeMyWndw,NewOSLAccessorProc(TextElemFromWndwAccessor),0,false);
  5901.         aevtErr = AEInstallObjectAccessor(cWord,             typeMyWndw,NewOSLAccessorProc(TextElemFromWndwAccessor),0,false);
  5902.         aevtErr = AEInstallObjectAccessor(cLine,             typeMyWndw,NewOSLAccessorProc(TextElemFromWndwAccessor),0,false);
  5903.         aevtErr = AEInstallObjectAccessor(cProperty,   typeMyWndw,NewOSLAccessorProc(PropertyFromWndwAccessor),0,false);
  5904.     
  5905.         aevtErr = AEInstallObjectAccessor(cProperty,     typeMyText,NewOSLAccessorProc(PropertyFromTextAccessor),0,false);
  5906.         aevtErr = AEInstallObjectAccessor(cChar,           typeMyText,NewOSLAccessorProc(TextElemFromTextAccessor),0,false);
  5907.         aevtErr = AEInstallObjectAccessor(cWord,           typeMyText,NewOSLAccessorProc(TextElemFromTextAccessor),0,false);
  5908.         aevtErr = AEInstallObjectAccessor(cSpot,           typeMyText,NewOSLAccessorProc(TextElemFromTextAccessor),0,false);
  5909.         aevtErr = AEInstallObjectAccessor(cLine,           typeMyText,NewOSLAccessorProc(TextElemFromTextAccessor),0,false);
  5910.         
  5911.         aevtErr = AEInstallObjectAccessor(cMenu,           typeNull,       NewOSLAccessorProc(MenuFromNullAccessor),    0,false);
  5912.         aevtErr = AEInstallObjectAccessor(cProperty,   typeMyMenu,     NewOSLAccessorProc(PropertyFromMenuAccessor),0,false);
  5913.         aevtErr = AEInstallObjectAccessor(cProperty,   typeMyMenuItem, NewOSLAccessorProc(PropertyFromMenuItemAccessor),0,false);
  5914.         aevtErr = AEInstallObjectAccessor(cMenuItem,   typeMyMenu,     NewOSLAccessorProc(MenuItemFromMenuAccessor),0,false);
  5915.         /* Now the coercion handlers */
  5916.  
  5917.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyAppl,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5918.         aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyWndw,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5919.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyText,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5920.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyTextProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5921.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyWindowProp,(AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5922.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyApplProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5923.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenu,      (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5924.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenuProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5925.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenuItem,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5926.       aevtErr = AEInstallCoercionHandler(typeObjectSpecifier,typeMyItemProp,  (AECoercionHandlerUPP)NewAECoerceDescProc(CoerceObjToAnything),0,true,false);
  5927.         
  5928.         /*now install the appropriate edition manager events*/
  5929.         
  5930.         aevtErr = AEInstallEventHandler( sectionEventMsgClass, sectionReadMsgID,   NewAEEventHandlerProc(DoReadSection), noRefCon, false) ;
  5931.         aevtErr = AEInstallEventHandler( sectionEventMsgClass, sectionWriteMsgID,  NewAEEventHandlerProc(DoWriteSection), noRefCon, false) ;
  5932.         aevtErr = AEInstallEventHandler( sectionEventMsgClass, sectionScrollMsgID, NewAEEventHandlerProc(DoScrollSection), noRefCon, false) ;
  5933.         aevtErr = AEInstallEventHandler( sectionEventMsgClass, sectionCancelMsgID, NewAEEventHandlerProc(DoCancelSection), noRefCon, false) ;
  5934.         
  5935.  } /* InitAppleEvents */
  5936.